2017 ACM暑期多校联合训练 - Team 4 1007 HDU 6073 Matching In Multiplication (模拟)
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
题意:
T组测试数据,给出两个集合用以表示顶点,左边为集合U,右边为集合V,每个顶点集合的大小为n,然后有n行输入,第i行的输入v1,w1,v2,w2,表示集合U中的第i个顶点与集合V中的v1,v2顶点分别相连,且两个边的权值为w1,w2。求两个集合的所有完美匹配的权值之和。一个完美匹配的权值为该匹配所有边的权值相乘,且数据保证至少存在一个完美匹配。
分析:
题目保证至少存在一个完美匹配,集合U中所有顶点的度数肯定都等于2,集合V中所有顶点的度数都大于等于1,那么对于V中度数为1的点,它们对所有完美匹配的权值的贡献w是不变的,因此,我们可以先处理V中度数为1的点。假设V中度数为1的点有k个,那么在U中就需要用k个点来与V中的k个度数为1的点匹配(这里可能不太好理解为什么U中也是k个点,解释一下原因,当V中度为1的点在集合U中到该边所对应的点u1之后,u1所对应的度也会减1,我们此时还要判断u1的度是否为1,为1 的话也要将此点进行相应的度为1的处理),然后U和V中都剩下m个点,m=(n-k)。此时我们可以知道U和V中所有点的度数都大于等于2了。(U中会存在度数为1的点吗?不存在的。U中顶点的度数只有我们在处理V中顶点的时候才会改变,那么当我们在处理V中度数为1的顶点N的时候,说明U中只有一个顶点M跟N相连,此时的处理只涉及N和M,所以U中别的顶点的度数还是跟初始一样。)继续,U中m个顶点的度数都是2,那么V中m个顶点的总度数就是2*m,又因为此时V中m个顶点的度数都大于等于2,那么我们可以知道V中m个顶点的度数都是2。此时我们可以发现,U∪V中的所有顶点的度数都是偶数,当完美匹配完成后,此时图中必然存在欧拉回路,也就是环。
对于一个环中,如O0->O1->O2->O3->O0,我们可以理解为(1)O0与O1匹配,O2和O3匹配,(2)O1和O2匹配,O3和O0匹配,也就是说,对于一个已经完成的匹配,它只存在两种方案。那么我们就可以在一个环中,通过枚举起点O0和O1来找出当前环的两种匹配权值X1,Y1。由于图中可能存在多个环,我们需要枚举U中m个点来找出所有的环,一个环对于结果的贡献值是common(X1+Y1);那么P个环就是common(X1+Y1)(X2+Y2)....*(XP+YP)=ans
ans就是最后的结果了。
代码:
#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 300000 + 10;
struct Edge
{
int nxt, to, w;///分别表示下一条边的编号,本边的终点,本边的权重
bool mrk;///一个标记
} e[N*4];
int T, n, head[N*2], cnt, deg[N*2], used[N*2];///deg存储每个点的度,used标记的是某一条边有没有被访问过
long long part[2];
void addedge(int u, int v, int w)///建立边之间的联系的话用的是头插法
{
e[++cnt].nxt = head[u];///指向上一条由节点u指出的边
e[cnt].to = v;
e[cnt].w = w;
e[cnt].mrk = 0;
head[u] = cnt;///改变头指针的值
}
void dfs(int cur, int idx)
{
used[cur] = 1;
for(int i=head[cur]; i; i=e[i].nxt)
{
if(e[i].mrk) continue;
e[i].mrk = e[i^1].mrk = 1;
(part[idx] *= e[i].w) %= mod;
dfs(e[i].to, 1-idx);
}
}
int main()
{
scanf("%d", &T);
while(T-- && scanf("%d", &n)!=EOF)
{
memset(head, 0, sizeof(head));
memset(deg, 0, sizeof(deg));
memset(used, 0, sizeof(used));
cnt = 1;
///将集合U中的点的编号看作1~n,集合V中的点的编号看作n+1~2*n
for(int u=1, v, w; u<=n; u++)
for(int i=1; i<=2; i++)
{
scanf("%d%d", &v,&w);
addedge(u, v+n, w);
addedge(v+n, u, w);
deg[u]++;
deg[v+n]++;
}
long long ans = 1;
queue<int> que;
for(int v=n+1; v<=n+n; v++) ///集合V里面才可能会有度为1的边
if(deg[v] == 1)
que.push(v);
while(!que.empty())///这样的话整个while循环下来,集合U和集合V里面的度为1的边都去掉了,只剩下度为2的边
{
int v = que.front();
que.pop();
used[v] = 1;
for(int i=head[v]; i; i=e[i].nxt)
{
if(e[i].mrk) continue;
e[i].mrk = e[i^1].mrk = 1;///相当于标记的是同一条边,因为每一条边都是分两个方向存下来的
used[ e[i].to ] = 1;
(ans *= e[i].w) %= mod;
for(int j=head[ e[i].to ]; j; j=e[j].nxt)///再从这个终点开始寻找,遇到变化后度为1的边,就把他们处理掉
{
e[j].mrk = e[j^1].mrk = 1;
deg[e[j].to]--;
if(deg[ e[j].to ] == 1) que.push(e[j].to);
}
}
}
///此时两个集合中都只有度为2的点,且两个集合的度的个数还相等
for(int u=1; u<=n; u++)
{
if(used[u]) continue;
part[0] = part[1] = 1;
dfs(u, 0);
(ans *= (part[0]+part[1]) % mod) %= mod;
}
printf("%lld\n", ans);
}
return 0;
}
2017 ACM暑期多校联合训练 - Team 4 1007 HDU 6073 Matching In Multiplication (模拟)的更多相关文章
- 2017 ACM暑期多校联合训练 - Team 4 1012 HDU 6078 Wavel Sequence (模拟)
题目链接 Problem Description Have you ever seen the wave? It's a wonderful view of nature. Little Q is a ...
- 2017 ACM暑期多校联合训练 - Team 9 1008 HDU 6168 Numbers (模拟)
题目链接 Problem Description zk has n numbers a1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk gen ...
- 2017 ACM暑期多校联合训练 - Team 5 1008 HDU 6092 Rikka with Subset (找规律)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- 2017 ACM暑期多校联合训练 - Team 3 1008 HDU 6063 RXD and math (莫比乌斯函数)
题目链接 Problem Description RXD is a good mathematician. One day he wants to calculate: ∑i=1nkμ2(i)×⌊nk ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
随机推荐
- PAT 甲级 1027 Colors in Mars
https://pintia.cn/problem-sets/994805342720868352/problems/994805470349344768 People in Mars represe ...
- Linux输入子系统:多点触控协议 -- multi-touch-protocol.txt768
转自:http://blog.csdn.net/droidphone/article/details/8434768 Multi-touch (MT) Protocol --------------- ...
- 一次性无重复配置VS项目插件属性的方法
在VS中需要使用opencv开源库或mysql等数据库时,为了能使用开源库或数据库的语言,需要添加库文件和包含目录等等.然而直接在[解决方案管理器]-->属性中配置的话,写下一个项目(解决方案) ...
- Sql Server统计报表案例
场景:查询人员指定年月工作量信息 USE [Test] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo ...
- 【前端学习笔记03】JavaScript对象相关方法及封装
//Object.create()创建对象 var obj = Object.create({aa:1,bb:2,cc:'c'}); obj.dd = 4; console.log(obj.cc); ...
- Spring Cloud Sleuth服务跟踪
监控 使用zipkin(https://zipkin.io/) 监控服务构建: (普通的springBoot项目) <!--引入的zipkinServer依赖--> <depende ...
- 51nod 1574 排列转换(贪心+鸽巢原理)
题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...
- 【Java】常用POI生成Excel文档设置打印样式
package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...
- Find the hotel HDU - 3193(RMQ)
题意: 有n个旅馆,从这n个旅馆中找出若干个旅馆,使得这若干个旅馆满足这样的条件:不能从其它和剩下的旅馆中找到一个价格和距离都小于这个旅馆的旅馆... 解析: 按price 排序,若price相同, ...
- Kinect实现简单的三维重建
Kinect想必大家已经很熟悉了,最近基于Kinect的创意应用更是呈井喷状态啊!看到很多国外大牛用Kinect做三维重建,其中最著名的要数来自微软研究院的Kinect Fusion了,可以看看下面这 ...