给你一个无向图 以及点的个数和边 每个节点只能用1 2 3 三个数字 求相邻
两个节点和为奇数 能否构成以及有多少种构成方法
#include<bits/stdc++.h> using namespace std; #define LL long long #define maxn 300005 const LL mod = 998244353; vector<int>q[maxn]; LL fa[maxn],sumx,sumy; bool flag = 0; LL poww(LL a,LL b){ LL ans = 1; while(b){ if(b%2){ ans = ans*a%mod; } b/=2; a = a*a%mod; } return ans%mod; } void dfs(int u,int deep){ if(flag||q[u].size()==0) return ; for(int j=0;j<q[u].size();j++){ int v = q[u][j]; if(fa[v]==-1){ fa[v] = 1-deep; sumx++; if(deep==1) sumy++; dfs(v,1-deep); }else{ if((1-deep)!=fa[v]){ flag = 1; return ; } } } } int main(){ int t; cin>>t; while(t--){ flag = 0; int n,m; scanf("%d%d",&n,&m); LL ans = 1; vector<int>s,z; for(int j=0;j<=n;j++){ q[j].clear(); fa[j]=-1; } for(int j=0;j<m;j++){ int u,v; scanf("%d%d",&u,&v); q[u].push_back(v); q[v].push_back(u); } for(int j=1;j<=n;j++){ sumx = 1,sumy = 1; if(fa[j]==-1){ fa[j]=0; dfs(j,0); ans = (ans*(poww(1LL*2,1LL*sumy)+poww(1LL*2,1LL*(sumx-sumy)))%mod)%mod; } } if(flag){ ans = 0; } cout<<ans<<endl; } }