HDU 6073 Matching In Multiplication —— 2017 Multi-University Training 4
Matching In Multiplication
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1389 Accepted Submission(s): 423
Little Q misunderstands the definition of bipartite graph, he thinks the size of U is equal to the size of V, and for each vertex p in U, there are exactly two edges from p. Based on such weighted graph, he defines the weight of a perfect matching as the product of all the edges' weight, and the weight of a graph is the sum of all the perfect matchings' weight.
Please write a program to compute the weight of a weighted ''bipartite graph'' made by Little Q.
In each test case, there is an integer n(1≤n≤300000) in the first line, denoting the size of U. The vertex in U and V are labeled by 1,2,...,n.
For the next n lines, each line contains 4 integers vi,1,wi,1,vi,2,wi,2(1≤vi,j≤n,1≤wi,j≤109), denoting there is an edge between Ui and Vvi,1, weighted wi,1, and there is another edge between Ui and Vvi,2, weighted wi,2.
It is guaranteed that each graph has at least one perfect matchings, and there are at most one edge between every pair of vertex.
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
typedef long long LL;
const int MAXN=6e5+;
const int N=3e5;
const LL MOD=;
struct Edge{
int to;
LL w;
};
vector<Edge> edge[MAXN];
int deg[MAXN];
LL s[];
void dfs(int node,int pos, bool ext){
//cout<<node<<' '<<s[0]<<' '<<s[1]<<' '<<deg[node]<<' '<<ext<<endl;
for(int i=;i<edge[node].size();i++){
int p=edge[node][i].to;
if(!deg[p])
continue; if(deg[p]==)
{
deg[node]--;
deg[p]--;
s[pos]=s[pos]*edge[node][i].w%MOD;
dfs(p, pos^, ext);
break;
}
else if(deg[node]==&°[p]==)
{
if(ext==false)
ext=true;
else{
deg[node]--;
deg[p]--;
s[pos]=s[pos]*edge[node][i].w%MOD;
return;
}
}
}
} int main()
{
//ifstream cin("ylq.txt");
int T;
cin>>T;
int n,v1,v2;
LL w1,w2;
Edge e1,e2;
while(T--)
{
memset(deg, , sizeof(deg));
//cin>>n;
scanf("%d", &n);
for(int i=;i<=N+n;i++){
edge[i].clear();
}
for(int i=;i<=n;i++){
//cin>>v1>>w1>>v2>>w2;
scanf("%d %lld %d %lld", &v1, &w1, &v2, &w2);
deg[v1+N]++;
deg[v2+N]++;
deg[i]+=; e1.to=v1+N;e1.w=w1;
e2.to=v2+N;e2.w=w2;
edge[i].push_back(e1);
edge[i].push_back(e2); e1.to=i;e2.to=i;
edge[v1+N].push_back(e1);
edge[v2+N].push_back(e2);
} LL left=;
queue<int> q;
for(int i=N;i<=n+N;i++)
if(deg[i]==)
q.push(i); int m;
while(!q.empty())
{
int p, pp;
m=q.front(); q.pop();
deg[m]=;
for(int i=;i<edge[m].size();i++){
p=edge[m][i].to;
if(!deg[p])
continue;
else
{
deg[p]=;
left=left*edge[m][i].w%MOD;
for(int k=;k<edge[p].size();k++){
pp=edge[p][k].to;
if(deg[pp]==) continue; deg[pp]--;
if(deg[pp]==)
q.push(pp);
}
}
} }
//cout<<'*'<<left<<'*'<<endl;
LL ans=left;
for(int i=;i<=n;i++){
if(!deg[i])
continue;
s[]=s[]=;
dfs(i, , );
ans=ans*(s[]+s[])%MOD;
} printf("%lld\n", ans);
}
}
我的代码里用入度出度判断是否走到重复点,看了好多人都是用vis判断的,感觉都差不多。。。。
HDU 6073 Matching In Multiplication —— 2017 Multi-University Training 4的更多相关文章
- HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4
/* HDU 6073 - Matching In Multiplication [ 图论 ] | 2017 Multi-University Training Contest 4 题意: 定义一张二 ...
- HDU 6073 Matching In Multiplication(拓扑排序)
Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K ( ...
- HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑
Matching In Multiplication Problem DescriptionIn the mathematical discipline of graph theory, a bipa ...
- 2017 ACM暑期多校联合训练 - Team 4 1007 HDU 6073 Matching In Multiplication (模拟)
题目链接 Problem Description In the mathematical discipline of graph theory, a bipartite graph is a grap ...
- HDU 6073 Matching In Multiplication(拓扑排序+思维)
http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...
- HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9
/* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...
- HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9
/* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...
- 2017 多校4 Matching In Multiplication(二分图)
Matching In Multiplication 题解: 首先如果一个点的度数为1,那么它的匹配方案是固定的,继而我们可以去掉这一对点.通过拓扑我们可以不断去掉所有度数为1的点. 那么剩下的图中左 ...
- hdu6073 Matching In Multiplication 分析+拓扑序
Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K ( ...
随机推荐
- 邮件解析 CNAME记录 A记录
域名配置 示例发信配置请至域名 service.i-test.cn DNS服务提供商处添加TXT记录,并保持SPF记录正确,否则会无法发信.*1.所有权验证类型 主机记录 主域名 记录值 状态TXT ...
- Flask框架视图多层装饰器问题
Flask中的app.route装饰器 我们知道,在flask框架中,我们的路由匹配就是通过有参装饰器来实现的,我们看一个简单的例子: from flask import Flask, render_ ...
- php面向对象重的抽象类,接口类与静态
static 静态 <?php class ren { public $name; public static $sex; static function shao() { echo " ...
- The server time zone value 'EDT' is unrecognized or represents more than one time zone
解决: (1)使用 server mysql start命令启动mysql (2)在mysql中执行show variables like '%time_zone%'; (3)输入select now ...
- spring4.1.8扩展实战之五:改变bean的定义(BeanFactoryPostProcessor接口)
本章我们继续实战spring的扩展能力,通过自定义BeanFactoryPostProcessor接口的实现类,来对bean实例做一些控制: 原文地址:https://blog.csdn.net/bo ...
- 什么时候需要用的Vue.nextTick()
什么时候需要用的Vue.nextTick() 你在Vue生命周期的created()钩子函数进行的DOM操作一定要放在Vue.nextTick()的回调函数中.原因是什么呢,原因是在created() ...
- [Linux] 003 分区
1. 磁盘分区 使用分区编辑器再磁盘上划分几个逻辑部分 不用类的目录与文件可以存储进不同的分区 2. 分区类型 主分区 最多只能有 4 个 扩展分区 最多只能有 1 个 主分区加扩展分区最多为 4 个 ...
- dp(最长公共上升子序列)
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...
- 基于各种基础数据结构的SPFA和各种优化
一.基于各种数据结构的SPFA 以下各个数据均为不卡SPFA的最短路模板:P3371 [模板]单源最短路径(弱化版)的测试时间 1.STL队列:用时: 1106ms / 内存: 8496KB #inc ...
- Ride to Office(贪心水题)
[题目链接] http://noi.openjudge.cn/ch0406/2404/ [算法] 一开始zz了,先按时间排序然后如果速度超过当前男主速度,且在男主到达目的地前超过男主则最终男主和这个人 ...