1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <bits/stdc++.h> using namespace std; const int N=300005; const int M=998244353; vector<int> G[N]; int vis[N],col[N],deg[N]; int cnt[2]; int kpow(int x,int y){ int ans=1; while(y){ if(y&1)ans=(1ll*ans*x)%M; x=(1ll*x*x)%M; y>>=1; } return ans; } int dfs(int x,int p=0){ vis[x]=1; col[x]=p; cnt[p]++; for(auto i:G[x]){ int f=1; if(vis[i]&&col[i]==col[x])return 0; if(!vis[i])f=dfs(i,!p); if(f==0)return 0; } return 1; } int main(){ int t; scanf("%d",&t); while(t--){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ G[i].clear(); vis[i]=col[i]=deg[i]=0; } for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); deg[u]++,deg[v]++; } int f=1,ans=1; for(int i=1;i<=n;i++){ if(deg[i]==0){ ans=(ans*3ll)%M; continue; } if(!vis[i]){ cnt[0]=cnt[1]=0; if(!dfs(i))f=0; else{ ans=(1ll*ans*(kpow(2,cnt[0])+kpow(2,cnt[1])))%M; } } if(f==0)break; } if(f==0)puts("0"); else printf("%d\n",ans); } return 0; }
|