D. Beautiful Graph
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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, 22 or 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
Copy
2 2 1 1 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4
output
Copy
4 0
Note
Possible ways to distribute numbers in the first test:
In the second test there is no way to distribute numbers.
点我传送
给出n个点m条边,可以给每个点赋值为1或2或3。现在询问有多少种赋值方法使得给出的图为 Beautiful Graph。
Beautiful Graph的定义为,每条边的两个端点相加为奇数。
要每条边的两端点相加为奇数,那么两个端点的值只可能是一奇一偶。这样的话问题就转化为了图染色的问题。DFS暴力的跑出奇数端点的数量(odd)和偶数端点的数量(eve),因为奇数是可以取1或3的,那么对于一个块的答案就是(2^odd+2^eve)。为什么说是一个块的答案呢,因为题目给出的图不一定是连通的,所以最终的答案应该是所有块的答案的乘积。
#include
#include
#include
using namespace std;
typedef long long LL;
int T;
int col[300005];
int n,m;
int odd,eve;
const LL mod=998244353;
LL mul[300005];
bool flag;
vectore[300005];
void DFS(int nx,int pre,int co)
{
if(!flag)return ;
col[nx]=co;
odd+=(co&1);
eve+=(co^1);
int Size=e[nx].size();
for(int i=0;i