Hdu 6073 Matching In Multiplication 二分图完美匹配
来源:互联网 发布:relex软件 编辑:程序博客网 时间:2024/05/17 23:37
Matching In Multiplication
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1855 Accepted Submission(s): 563
Problem Description
In the mathematical discipline of graph theory, a bipartite graph is a graph whose vertices can be divided into two disjoint sets U and V (that is, U and V are each independent sets) such that every edge connects a vertex in U to one in V . Vertex sets U and V are usually called the parts of the graph. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles. A matching in a graph is a set of edges without common vertices. A perfect matching is a matching that each vertice is covered by an edge in the set.
Little Q misunderstands the definition of bipartite graph, he thinks the size ofU 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.
Little Q misunderstands the definition of bipartite graph, he thinks the size of
Please write a program to compute the weight of a weighted ''bipartite graph'' made by Little Q.
Input
The first line of the input contains an integer T(1≤T≤15) , denoting the number of test cases.
In each test case, there is an integern(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 nextn 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.
In each test case, there is an integer
For the next
It is guaranteed that each graph has at least one perfect matchings, and there are at most one edge between every pair of vertex.
Output
For each test case, print a single line containing an integer, denoting the weight of the given graph. Since the answer may be very large, please print the answer modulo 998244353 .
Sample Input
122 1 1 41 4 2 3
Sample Output
16
Source
2017 Multi-University Training Contest - Team 4
给你一个二分图,左右两部分点数相等,左边每个点的度数都为2,每条边都有边权.保证至少存在一种完美匹配的方案,定义一种匹配方案的权值为所有匹配边的权值乘积,求所有完美匹配方案的权值和。
观察发现,对于某个度数为1的点,这个点所连的边必须被选。于是,先把所有必选边选出来,这些边被选择,代表它的起点和终点的匹配边已经确定,则可以把这些点的所有边删去,又出现了一批度数为1的点……就这样一直删,先把所有的必选边找出。设这些必选边的乘积为tot.
剩下的没有确定匹配边的所有点,必定度数为2. 至于为什么,画图画出来的。。。这样,我们可以对每个连通块分别求匹配方案。观察发现,一共只有两种方案,即从起点开始任选边dfs,每次dfs一条边,这样一笔画经过整个连通块,在路径上交替选边分别构成了两种方案。有n个连通块时,每个连通块两种方案,总共2^n种方案,设每个块的两种方案当中,每种的边权乘积分别是sn0,sn1,则ans=tot*(s10+s11)*(s20+s21)*...*(sn0+sn1)。
要注意的是,无向图删边时,要将两个方向的边都删去。
是一道好题,思路不算太难想,具体实现不算太简单也不算太难。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <deque>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=600005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f,mod=998244353; const ld pi=acos(-1.0L);int d[maxn],head[maxn];bool va[maxn];int num;ll s1,s0;struct Edge {int from,to,pre;ll dist;};Edge edge[maxn*2];void addedge(int from,int to,ll dist) {edge[num]=(Edge){from,to,head[from],dist};head[from]=num++;edge[num]=(Edge){to,from,head[to],dist};head[to]=num++;}void dfs(int now,int p) {ll ans=0;va[now]=1;for (int i=head[now];i!=-1;i=edge[i].pre) {if (edge[i].dist==0) continue;if (p==0) {s0*=edge[i].dist;s0%=mod; } else {s1*=edge[i].dist;s1%=mod; }edge[i].dist=edge[i^1].dist=0;dfs(edge[i].to,p^1);return;}}int main() {int cas;scanf("%d",&cas);while (cas--) { int i,j,k,y;ll n,e,tot=1,m;scanf("%lld",&n);mem0(d);num=0;memset(head,-1,sizeof(head));for (i=1;i<=n;i++) {scanf("%d%lld",&y,&e);d[i]=2;d[y+n]++;addedge(i,y+n,e);scanf("%d%lld",&y,&e);d[y+n]++;addedge(i,y+n,e);}queue<int> q;mem0(va);m=0;for (i=n+1;i<=2*n;i++) {if (d[i]==1) {q.push(i);va[i]=1;for (j=head[i];j!=-1;j=edge[j].pre) {if (!va[edge[j].to]) q.push(edge[j].to);va[edge[j].to]=1;tot*=edge[j].dist;tot%=mod;edge[j].dist=edge[j^1].dist=0;}}}while (!q.empty()) {int now=q.front();q.pop();for (j=head[now];j!=-1;j=edge[j].pre) {if (edge[j].dist==0) continue;edge[j].dist=edge[j^1].dist=0;int to=edge[j].to;d[to]--;if (d[to]==1&&!va[to]) {q.push(to);va[to]=1;for (k=head[to];k!=-1;k=edge[k].pre) {if (edge[k].dist==0) continue;if (!va[edge[k].to]) q.push(edge[k].to);va[edge[k].to]=1;tot*=edge[k].dist;tot%=mod;edge[k].dist=edge[k^1].dist=0;}}}}for (i=1;i<=n;i++) {if (!va[i]) {s1=s0=1;dfs(i,0);tot*=s1+s0;tot%=mod;}}printf("%lld\n",tot);}return 0;}
阅读全文
0 0
- Hdu 6073 Matching In Multiplication 二分图完美匹配
- HDU 6073 Matching In Multiplication (拓扑排序+搜索 求二分图所有完美匹配价值和)
- HDU 6073 Matching In Multiplication
- HDU 6073 Matching In Multiplication
- HDU-6073 Matching In Multiplication
- hdu 6073 Matching In Multiplication
- hdu 6073 Matching In Multiplication (二分图与拓补排序)
- Matching In Multiplication HDU
- hdu 6073 Matching In Multiplication [dfs]
- HDU 6073 Matching In Multiplication(机智)
- Matching In Multiplication(HDU 6073)
- HDU_6073 Matching In Multiplication 【二分图&&拓扑排序&&DFS】
- HDU 6073 Matching In Multiplication(强连通+拓扑)
- HDU-6073 Matching In Multiplication(拓扑+dfs)
- HDU 6073 Matching In Multiplication 思维(拓扑)
- hdu 6073 Matching In Multiplication(2017 Multi-University Training Contest
- HDU 6073 Matching In Multiplication(拓扑+思维)
- 【多校训练】hdu 6073 Matching In Multiplication. 拓扑+dfs
- iPhone 8/8 Plus体验:一个时代的收官!
- USACO注册以及使用
- Hash Tables: String Search--Data Structure
- [uva-572]Oil Deposits (BFS和DFS两种
- 巴什博奕
- Hdu 6073 Matching In Multiplication 二分图完美匹配
- node.js 连接 mssql 代码
- Nand Flash学习笔记1-Read的介绍
- 多态与O2
- Sqoop导入hive数据库NULL值处理
- unity 音乐可视化
- 抽象类和接口
- android pull xml
- LeetCode #49