D - Beautiful Graph
CodeForces - 1093D
You are given an undirected unweighted graph consisting of nn vertices and mm edges.
You have to write a number on each vertex of the graph. Each number should be 11, 22or 33. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd.
Calculate the number of possible ways to write numbers 11, 22 and 33 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353998244353.
Note that you have to write exactly one number on each vertex.
The graph does not have any self-loops or multiple edges.
Input
The first line contains one integer tt (1≤t≤3⋅1051≤t≤3⋅105) — the number of tests in the input.
The first line of each test contains two integers nn and mm (1≤n≤3⋅105,0≤m≤3⋅1051≤n≤3⋅105,0≤m≤3⋅105) — the number of vertices and the number of edges, respectively. Next mm lines describe edges: ii-th line contains two integers uiui, vivi (1≤ui,vi≤n;ui≠vi1≤ui,vi≤n;ui≠vi) — indices of vertices connected by ii-th edge.
It is guaranteed that ∑i=1tn≤3⋅105∑i=1tn≤3⋅105 and ∑i=1tm≤3⋅105∑i=1tm≤3⋅105.
Output
For each test print one line, containing one integer — the number of possible ways to write numbers 11, 22, 33 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353998244353.
Example
Input
2
2 1
1 2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
4
0
Note
Possible ways to distribute numbers in the first test:
- the vertex 11 should contain 11, and 22 should contain 22;
- the vertex 11 should contain 33, and 22 should contain 22;
- the vertex 11 should contain 22, and 22 should contain 11;
- the vertex 11 should contain 22, and 22 should contain 33.
In the second test there is no way to distribute numbers.
题意:
给你n个节点,m个边的无向图。
每人一个节点可以填1,2,3 中的任意一个。
问你有多少种填数字的方案,使每一条边连接的两个节点填的数字相加为奇数。
思路:
因为一个奇数+一个偶数=奇数。
所以如果有合法的填充方案就是有合法的黑白染色方案。
那么我们先用经典的“黑白染色-判断法”判断是否能有合法方案。
如果没有直接输出0
如果可以成功黑白染色,再计算方案数。
我们通过可以推出
每一个连通块中的方案数是 2^(填奇数的节点个数)+2^(填偶数的节点个数)
填奇数的节点个数和偶数的个数可以通过dfs得出。
如果一个连通块中只有一个节点,那么方案数应该是3.
每一个联通的答案的乘积就是总方案数。
记得取模即可。
细节见代码:
#include
#include
#include
#include
#include
#include
#include
#include