CodeForces - 1093D Beautiful Graph 匹配问题

You are given an undirected unweighted graph consisting of n vertices and m

edges.

You have to write a number on each vertex of the graph. Each number should be 1

, 2 or 3

. 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 1

, 2 and 3 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353

.

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 t

(1≤t≤3⋅105

) — the number of tests in the input.

The first line of each test contains two integers n

and m (1≤n≤3⋅105,0≤m≤3⋅105) — the number of vertices and the number of edges, respectively. Next m lines describe edges: i-th line contains two integers ui, vi (1≤ui,vi≤n;ui≠vi) — indices of vertices connected by i

-th edge.

It is guaranteed that ∑i=1tn≤3⋅105

and ∑i=1tm≤3⋅105

.

Output

For each test print one line, containing one integer — the number of possible ways to write numbers 1

, 2, 3 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353

.

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:

  1. the vertex 1

should contain 1, and 2 should contain 2

  • ;
  • the vertex 1
  • should contain 3, and 2 should contain 2
  • ;
  • the vertex 1
  • should contain 2, and 2 should contain 1
  • ;
  • the vertex 1
  • should contain 2, and 2 should contain 3
    1. .

    In the second test there is no way to distribute numbers.

题意:1 2 3 ,每个点给一个值,要求相邻两点权值加和为奇数,求方案数

题解:假设有两个数1 2的话,很明显,如果存在合理方案,那么就肯定为两种,1和2换一下即可,因为这里多了一个3,所以,

如果权值为奇数的为cnt1个,偶数的cnt2个,此时方案数为2^cnt1, 然后奇偶一换,方案数为2^cnt2

所以方案总数为2^cnt1+2^cnt2,因为没说是所有的点联通,所以每块乘起来即可,若遇到不符合标记一下即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const ll mod=998244353; 
const int N=3e5+10;
#define pb push_back
int n,m,vis[N];
vector<int> v[N];
int cnt1,cnt2;
int flag;
void dfs(int u)
{
	if(vis[u]==1) cnt1++;
	else cnt2++;
	for(int i=0;i<v[u].size();i++)
	{
		int to=v[u][i];
		if(vis[to])
		{
			if(vis[to]==vis[u]) flag=1;  //   遇见不符合 标记
		}
		else
		{
			vis[to]=3-vis[u];
			dfs(to);
		}
	}
}
ll ksm(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int x,y;
		scanf("%d%d",&n,&m);
		for(int i=0;i<=n;i++)vis[i]=0,v[i].clear();
		flag=0;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			v[x].pb(y);
			v[y].pb(x); 
		}
		ll ans=1;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) continue;
			vis[i]=1;
			cnt1=cnt2=0;
			dfs(i);
			if(flag) break;
			ans=(ans*(ksm(2,cnt1)+ksm(2,cnt2))%mod)%mod;
		}
		if(flag) cout<<"0\n";
		else cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/85036902
今日推荐