2017 Multi-University Training Contest

来源:互联网 发布:淘宝男士羽绒服加厚 编辑:程序博客网 时间:2024/05/18 03:06

题目传送门

Matching In Multiplication

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 619 Accepted Submission(s): 174

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(1≤T≤15), denoting the number of test cases.

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.

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
1
2
2 1 1 4
1 4 2 3

Sample Output
16

Source
2017 Multi-University Training Contest - Team 4

比赛时没有做出来,怪我图论基本功太差,太菜了,解题技巧也不够熟练,此类题目练习也并不多,虽然看题目就知道和二分图匹配相关,但是还是无从下手,以后要多多练习,不能一直都是弱弱吧(苦笑脸!!)。比赛结束后看了题解才恍然大悟,不过还是花了比较多的时间才写出来。
官方题解:
首先如果一个点的度数为1,那么它的匹配方案是固定的,继而我们可以去掉这一对点。通过拓扑我们可以不断去掉所有度数为1的点。
那么剩下的图中左右各有m个点,每个点度数都不小于2,且左边每个点度数都是2,而右侧总度数是2m,因此右侧只能是每个点度数都是2。这说明这个图每个连通块是个环,在环上间隔着取即可,一共两种方案。
AC代码:

#pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 6e5 + 1000;const ll mod = 998244353;int G[N],du[N],vis[N];int tot,u,v,n,st;ll d,ans,t1,t2;struct Edge {    int to;    ll w;    int next;    Edge(int to_=0,ll w_=0,int next_=0):to(to_),w(w_),next(next_){}}E[N*2];struct node {    int u;    int flag;};void init() {    memset(du,0,sizeof(du));    memset(G,0,sizeof(G));    memset(vis,0,sizeof(vis));    tot = 1;  ans = 1;}inline void addEdge(int f,int t,ll dist) {    E[tot] = Edge(t,dist,G[f]);    G[f] = tot ++;    E[tot] = Edge(f,dist,G[t]);    G[t] = tot ++;}void topSort() {    queue<node>Q;    for(int i=1;i<=n;i++)        if(du[i] == 1)        Q.push((node){i,0});//度为1的点进行匹配     while(!Q.empty()) {        node now = Q.front();         Q.pop();        int u = now.u;        if(vis[u])        continue;        vis[u] = 1;        for(int i=G[u];i;i=E[i].next) {            int v = E[i].to;            if(vis[v])            continue;            du[v] --;            //点v的度-1,因为点u会进行匹配             if(du[v] <= 1) {                if(!now.flag)ans = ans * E[i].w % mod;                  Q.push((node){v,!now.flag});            }        }    } }void dfs(int u,int dep) {    vis[u] = 1;    bool flag = true;    for(int i=G[u];i;i=E[i].next)     {        int v = E[i].to;        if(vis[v])continue;        flag = false;        if(dep%2)        t1 = t1 * E[i].w % mod;        else         t2 = t2 * E[i].w % mod;        dfs(v, dep + 1);    }    if(flag) {        for(int i=G[u];i;i=E[i].next)             if(E[i].to == st)           {                if(dep%2)t1 = t1 * E[i].w % mod;                else t2 = t2 * E[i].w % mod;            }    }}int main() {    int t;     scanf("%d",&t);    while(t--) {        init();        scanf("%d",&n);        for(int i=1;i<=n;i++) {            for(int j=0;j<2;j++) {                scanf("%d%lld",&v,&d);                v = v + n;                //v集合的点加上n防止和u集合的点有交集                 du[v] ++, du[i] ++;                //记录每个点的度                 addEdge(i,v,d);            }        }        n = 2 * n;//(1-n)为u,(n+1-2n)为V         topSort();//拓扑一下,将度为1的先匹配好         for(int i=1;i<=n;i++) {            if(!vis[i]) {                t1 = t2 = 1; st = i;                /*在环上间隔着取,即st为结束点也是开始点(一个环)t1,t2(两种方案)*/                dfs(i,1);                ll tmp = (t1 + t2) % mod;                ans = ans * tmp % mod;            }        }        printf("%lld\n",ans);    }    return 0;}
原创粉丝点击