HDU 6073 Matching In Multiplication(拓扑排序)
Matching In Multiplication
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1127    Accepted Submission(s): 325

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.
2
2 1 1 4
1 4 2 3
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define qwer 2e18
using namespace std;
typedef long long ll;
const int N = 6e5+;
const int M = ;
const int mod = ;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,s;
int vis[N],in[N];
ll ans[];
vector<pii>edg[N];
ll topSort(){
queue<int>q;
ll ret=;
for(int i=n+;i<=n+n;i++){
if(in[i]==){
q.push(i);
vis[i]=;
}
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<edg[u].size();i++){
int v=edg[u][i].first;
if(vis[v])continue;
if((--in[v])==)q.push(v),vis[v]=;
if(u>n)ret=(ret*1LL*edg[u][i].second)%mod;
}
}
return ret;
}
void dfs(int u,int ty,int fa){
vis[u]=;
for(int i=;i<edg[u].size();i++){
int v=edg[u][i].first;
if(v==s&&v!=fa)ans[ty]=(ans[ty]*1LL*edg[u][i].second)%mod;
if(vis[v])continue;
ans[ty]=(ans[ty]*1LL*edg[u][i].second)%mod;
dfs(v,ty^,u);
}
}
int main(){
//freopen("de.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<N;i++)vis[i]=in[i]=,edg[i].clear();
for(int i=,v1,w1,v2,w2;i<=n;i++){
scanf("%d%d%d%d",&v1,&w1,&v2,&w2);
v1+=n;v2+=n;
edg[i].pb(mp(v1,w1));
edg[v1].pb(mp(i,w1));
edg[i].pb(mp(v2,w2));
edg[v2].pb(mp(i,w2));
in[i]+=;
in[v1]++;in[v2]++;
}
ll anss=topSort();
for(s=;s<=n;s++){
if(!vis[s]){
ans[]=ans[]=;
dfs(s,,);
anss=anss*((ans[]+ans[])%mod)%mod;
}
}
printf("%lld\n",anss);
}
return ;
}
HDU 6073 Matching In Multiplication(拓扑排序)的更多相关文章
- 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(拓扑排序+思维)
		
http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...
 - HDU 6073 Matching In Multiplication  dfs遍历环 + 拓扑
		
Matching In Multiplication Problem DescriptionIn the mathematical discipline of graph theory, a bipa ...
 - 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 ( ...
 - 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.3342 Legal or Not (拓扑排序 TopSort)
		
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
 - HDU.1285 确定比赛名次 (拓扑排序 TopSort)
		
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
 - HDU 4857 逃生 (反向拓扑排序 & 容器实现)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
 - ACM:   HDU 1285 确定比赛名次 - 拓扑排序
		
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
 
随机推荐
- web开发中防止SQL注入
			
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编写时的疏忽,通过SQL语句,实现无账号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...
 - 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局
			
[题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...
 - shell 给未定义的变量设定默认值  ${parameter:-word}
			
参考: [ Unix & Linux ] Shell Demo $echo ${JENKINS_VERSION:-2.7.4} 2.7.4 $JENKINS_VERSION=2.99 $ech ...
 - 网络协议之HTTP协议
			
HTTP协议详解(真的很经典) 转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HT ...
 - perl登录ssh
			
use warnings; use strict; use Net::SSH::Perl; my $host = '192.168.255.128'; my $username = 'root'; m ...
 - perl 在win下输出中文乱码问题
			
use utf8; my $name = '你好'; binmode(STDOUT, ":encoding(gbk)"); print $name,"\n"; ...
 - rabbitmq之核心构架和原理总结(四)
			
前言 前面博文已经将安装配置和站点管理介绍了,现在开始正式学习rabbitmq的使用了: rabbitMQ的构架 rabbitmq作为消息队列,一条消息从发布到订阅消费的完整流程为: 消息 --> ...
 - epoll内核源码分析
			
转载:https://www.nowcoder.com/discuss/26226?type=0&order=0&pos=27&page=1 /* * fs/eventpo ...
 - skb管理函数之alloc_skb、dev_alloc_skb、kfree_skb、dev_kfree_skb、consume_skb
			
alloc_skb--分配skb dev_alloc_skb--分配skb,通常被设备驱动用在中断上下文中,它是alloc_skb的封装函数,因为在中断处理函数中被调用,因此要求原子操作(GFP_AT ...
 - Laravel 5.2 三、中间件、视图与 Blade 模板引擎
			
一.中间件 Laravel 的 HTTP 中间件提供了对路由的一层过滤和保护.下面模拟一下用中间件验证后台登录. 1. 创建中间件 cmd 窗口进入项目目录,使用 artisan 命令创建 php a ...