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

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
【题意】给你一个二分图,每一集合里的 点数量 都为n,且其中一个集合里每个点的度数都为2.然后对于每一种完美匹配,算出边权值的乘积,然后再将每一种匹配的乘积加起来,输出最后结果。
【分析】U集合里的点 度数都为二,V集合里的点度数未知,但加起来肯定为2*n,对于V集合里度数为1的点,它的匹配对象是固定的,所以我们用拓扑排序将度数为1的点全部挖出,算出乘积res。然后对于剩下 的图,假设总节点为2*m,则V集合总度数为2*m,由于此时V集合里已经,没有度数为1的点,所以V集合里点的度数都为2,这说明这个图每个连通块是个环,在环上间隔着取即可,一共两种方案。比如对于两个联通块,他俩的两种方案乘积分别是(a1,a2),(b1,b2),则答案为a1*b1+a1*b2+a2*b1+a2*b2,化简后为(a1+a2)*(b1+b2),最后在乘以res即可。
#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(拓扑排序)的更多相关文章

  1. HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4

    /* HDU 6073 - Matching In Multiplication [ 图论 ] | 2017 Multi-University Training Contest 4 题意: 定义一张二 ...

  2. HDU 6073 Matching In Multiplication(拓扑排序+思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...

  3. HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑

    Matching In Multiplication Problem DescriptionIn the mathematical discipline of graph theory, a bipa ...

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

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

  6. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  7. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  8. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

随机推荐

  1. [Luogu 3958] NOIP2017 D2T1 奶酪

    题目链接 人生第一篇题解,多多关照吧. 注意事项: 1.多组数据,每次要先初始化. 2.因为涉及到开根,所以记得开double. 整体思路: 建图,判断「起点」与「终点」是否连通. 方法可选择搜索(我 ...

  2. 【51NOD-0】1137 矩阵乘法

    [算法]简单数学 [题解] 对于A*B=C C中第i行第j列的数字由A中第i行和B中的j列的数字各自相乘后相加得到. 所以两个矩阵能相乘要求A的列数等于B的行数,复杂度为O(n3). #include ...

  3. JS之window对象

    window对象 window属性: opener:打开当前窗口的源窗口,如果这个窗口是由别的网页点击链接跳转过来的,或者是从另外一个页面点击打开窗口打开的,opener就是找到源页面的.如果当前窗口 ...

  4. 一些达成共识的JavaScript编码风格约定【转】

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  5. perl HTML::LinkExtor模块(2)

    use LWP::Simple; use HTML::LinkExtor; $html_code = get("https://tieba.baidu.com/p/4929234512&qu ...

  6. Python模块学习 - ConfigParser

    配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...

  7. Python学习笔记 - day12 - Python操作NoSQL

    NoSQL(非关系型数据库) NoSQL,指的是非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称.用于超大规模数据的存储.(例如 ...

  8. 使用Redirector插件解决googleapis公共库加载的问题【转】

    转自:http://www.cnblogs.com/kari/p/5860371.html 最近访问一些面向国外的网站总是会出现ajax.googleaips.com无法加载的情况.以下为加载stac ...

  9. linux和ubuntu防火墙相关命令

    1.永久有效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2.即刻生效 开启: service iptables start 关闭: se ...

  10. 算法题之找出数组里第K大的数

    问题:找出一个数组里面前K个最大数. 解法一(直接解法): 对数组用快速排序,然后直接挑出第k大的数.这种方法的时间复杂度是O(Nlog(N)).N为原数组长度. 这个解法含有很多冗余,因为把整个数组 ...