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 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.
 

Input
The first line of the input contains an integer T(1T15), denoting the number of test cases.

In each test case, there is an integer n(1n300000) 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(1vi,jn,1wi,j109), 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.
 

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;}



原创粉丝点击