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

题意:

  给你一个图,n点 2*n边,有边权。

  左边的1~n个点出度都为2,且都连向右边的点,两点之间,没有重边,求出每种完美匹配下各边乘积的总和

题解:

  不好好写就会wa死你哦,咕咕咕

  首先如果一个点的度数为11,那么它的匹配方案是固定的,继而我们可以去掉这一对点。通过拓扑我们可以不断去掉所有度数为11的点。

  那么剩下的图中左右各有mm个点,每个点度数都不小于22,且左边每个点度数都是22,而右侧总度数是2m2m,

  因此右侧只能是每个点度数都是22。这说明这个图每个连通块是个环,在环上间隔着取即可,一共两种方案。

  时间复杂度O(n)O(n)

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e6+, M = 1e3+,inf = 2e9; const LL mod = 998244353LL; queue<pii > q;
int vis[N],done[N],d[N],n,T,zhong;
LL ans1,ans2;
vector<pii > G[N];
void init() {
for(int i = ; i <= *n; ++i)
G[i].clear(),vis[i] = ,done[i] = ,d[i] = ;
}
void dfs(int u,int f,int p) {
vis[u] = ;
int flag = ;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i].first;
if(vis[to]) continue;
flag = ;
if(!p) ans1 = 1LL * ans1 * G[u][i].second % mod;
else ans2 = 1LL * ans2 * G[u][i].second % mod;
dfs(to,u, - p);
}
if(flag) {
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i].first;
if(to != zhong) continue;
if(!p) ans1 = 1LL * ans1 * G[u][i].second % mod;
else ans2 = 1LL * ans2 * G[u][i].second % mod;
}
}
}
void make_faiil(int u,int f,int p) {
done[u] = ;
vis[u] =;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i].first;
if(to == f || done[to]) continue;
make_faiil(to,u, - p);
}
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
init();
for(int i = ; i <= n; ++i) {
int x,y;
scanf("%d%d",&x,&y);
d[i] += ;
d[x + n] += ;
G[i].push_back(MP(x + n,y));
G[x + n].push_back(MP(i,y)); scanf("%d%d",&x,&y);
d[i] += ;
d[x + n] += ;
G[i].push_back(MP(x + n,y));
G[x + n].push_back(MP(i,y));
}
int ok = ;
while(!q.empty()) q.pop();
for(int i = n+; i <= *n; ++i) {
if(d[i] == ) {
q.push(MP(i,));
vis[i] = ;
}
}
LL ans = 1LL;
while(!q.empty()) {
pii k = q.front();
q.pop();
for(int i = ; i < G[k.first].size(); ++i) {
int to = G[k.first][i].first;
if(vis[to]) continue;
d[to] -= ;
if(d[to] == ) {
if(!k.second)ans = ans * G[k.first][i].second % mod;
q.push(MP(to,!k.second));
vis[to] = ;
}
}
}
//cout<<ans<<endl;
LL tmp1 = ans,tmp2 = ;
for(int i = ; i <= *n; ++i) {
if(!vis[i]){
// cout<<"huasndina " << i<<endl;
ans1 = 1LL,ans2 = 1LL;
zhong = i;
dfs(i,-,);
LL tmptmp = (ans1 + ans2) % mod;
ans = ans * tmptmp % mod;
}
}
printf("%lld\n",(ans)%mod);
}
return ;
} /*
10
5
1 1 2 3
1 2 3 4
3 5 4 7
3 6 4 8
4 9 5 10
4920
*/

HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑的更多相关文章

  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(拓扑排序)

    Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K ( ...

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

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

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

  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. hdu6073 Matching In Multiplication 分析+拓扑序

    Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K ( ...

  7. 2017 多校4 Matching In Multiplication(二分图)

    Matching In Multiplication 题解: 首先如果一个点的度数为1,那么它的匹配方案是固定的,继而我们可以去掉这一对点.通过拓扑我们可以不断去掉所有度数为1的点. 那么剩下的图中左 ...

  8. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  9. HDU 2553(N皇后)(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn. ...

随机推荐

  1. PAT天梯赛练习题——L3-005. 垃圾箱分布(暴力SPFA)

    L3-005. 垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁 ...

  2. LightOJ——1066Gathering Food(BFS)

    1066 - Gathering Food   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB W ...

  3. P3147 [USACO16OPEN]262144 (贪心)

    题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-262,144),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: ...

  4. 程序自动分析(codevs 4600)

    题目描述 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi ...

  5. @Java Web 程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码

    一.前言 这篇算是类加载器的实战第五篇,前面几篇在这里,后续会持续写这方面的一些东西. 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 ...

  6. WEB学习-HTML的骨架

    HTML的标准骨架 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  7. Android-一张图理解MVP的用法

    M和V通过P交互,M做了两件事,开启子线程做耗时操作,然后使用原生的Hander方式切回主线程回调结果给P. M做的两件事也可以使用比较流行的rxjava实现: 备注:图片不清晰可以看这里

  8. Go --- GC优化经验

    不想看长篇大论的,这里先给个结论,go的gc还不完善但也不算不靠谱,关键看怎么用,尽量不要创建大量对象,也尽量不要频繁创建对象,这个道理其实在所有带gc的编程语言也都通用. 想知道如何提前预防和解决问 ...

  9. centos下开启htaccess

    不知道原本 centOS是否默认支持 .htaccess 可能是因为我总弄配置文件无意中给搞坏了 今天要用到就查了下怎么开启 想要顺利开启需注意以下几点, 这几点都是在httpd.conf 这个配置文 ...

  10. 使用datatables实现列宽设置、水平滚动条、显示某列部分内容

    示例 1.//使用 columnDefs 给列设置宽度 $('#example').DataTable( { "columnDefs": [ //给第一列指定宽度为表格整个宽度的2 ...