[关闭]
@Chilling 2017-08-09T14:58:31.000000Z 字数 3166 阅读 828

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, denoting the number of test cases.

In each test case, there is an integer in the first line, denoting the size of U. The vertex in U and V are labeled by .

For the next n lines, each line contains 4 integers , denoting there is an edge between and , weighted , and there is another edge between and , weighted .

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

题意:给你一个二分图,左边为U,右边为V,输入一个n,表示U和V集合里分别有n个点,接下来n行,每行四个数,,该行为第i行,表示相连,边权为相连,边权为。那么也就是说,U集合中的每个点的度都为2,V集合不确定。将连线方案的边权相乘得到一个值,求所有方案的值的和为多少。

分析:首先找出V集合中度为1的点,那么它就只有一种连线,不断拓扑下去,将所有度为1的点都连好之后,剩下所有点的度都为2了,那么U和V连接一定能形成若干个简单环,每个环就有两种方案(间隔选取边)。


  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn = 6e5 + 10;
  5. const int mod = 998244353;
  6. int in[maxn], n, f;
  7. int vis[maxn];
  8. LL ans[2];
  9. struct node
  10. {
  11. int to, len;
  12. node (int to = 0, int len = 0) : to (to), len (len) {}
  13. };
  14. vector<node> V[maxn];
  15. queue<int> q;
  16. LL topsort()
  17. {
  18. LL ans = 1;
  19. for (int i = n + 1; i <= 2 * n; i++)
  20. if (in[i] == 1)
  21. {
  22. vis[i] = 1;
  23. q.push (i);
  24. }
  25. while (!q.empty() )
  26. {
  27. int u = q.front();
  28. q.pop();
  29. vis[u] = 1, in[u]--;
  30. for (int i = 0; i < V[u].size(); i++)
  31. {
  32. int v = V[u][i].to;
  33. if (vis[v]) continue;
  34. if (u > n) ans = ans * (LL) V[u][i].len % mod;
  35. in[v]--;
  36. if (in[v] == 1) q.push (v);
  37. }
  38. }
  39. return ans;
  40. }
  41. void dfs (int rt, int fa, int u, int f)
  42. {
  43. vis[u] = 1;
  44. int flag = 0;
  45. for (int i = 0; i < V[u].size(); i++)
  46. {
  47. int v = V[u][i].to;
  48. int len = V[u][i].len;
  49. if (v == rt && v != fa) ans[f] = ans[f] * (LL) len % mod;
  50. if (vis[v]) continue;
  51. flag = 1;
  52. ans[f] = ans[f] * (LL) len % mod;
  53. dfs (rt, u, v, f ^ 1);
  54. }
  55. }
  56. void clr()
  57. {
  58. memset (in, 0, sizeof (in) );
  59. memset (vis, 0, sizeof (vis) );
  60. while (!q.empty() ) q.pop();
  61. for (int i = 0; i <= n * 2; i++)
  62. V[i].clear();
  63. }
  64. int main()
  65. {
  66. int t;
  67. scanf ("%d", &t);
  68. while (t--)
  69. {
  70. scanf ("%d", &n);
  71. clr();
  72. for (int u = 1; u <= n; u++)
  73. {
  74. int v1, len1, v2, len2;
  75. scanf ("%d %d %d %d", &v1, &len1, &v2, &len2);
  76. in[v1 + n]++, in[v2 + n]++, in[u] = 2;
  77. V[u].push_back (node (v1 + n, len1) );
  78. V[u].push_back (node (v2 + n, len2) );
  79. V[v1 + n].push_back (node (u, len1) );
  80. V[v2 + n].push_back (node (u, len2) );
  81. }
  82. LL ans1 = topsort();
  83. for (int i = 1; i <= 2 * n; i++)
  84. {
  85. if (vis[i]) continue;
  86. ans[0] = ans[1] = 1;
  87. dfs (i, 0, i, 0);
  88. ans1 = (ans1 * ( (ans[0] + ans[1]) % mod) ) % mod;
  89. }
  90. printf ("%lld\n", ans1);
  91. }
  92. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注