UVA-11396 Claw Decomposition (二分图判定)
题目大意:给一张无向图,能否把它分成若干个“爪”,即,一个点有三个子节点。
题目分析:每个点的度数3是已知的,只需判断一下是不是二分图即可。
代码如下:
# include<iostream> # include<cstdio> # include<vector> # include<cstring> # include<algorithm> using namespace std; # define REP(i,s,n) for(int i=s;i<n;++i) # define CL(a,b) memset(a,b,sizeof(a)) const int N=305; vector<int>G[N]; int vis[N]; bool dfs(int u) { for(int i=0;i<G[u].size();++i){ int v=G[u][i]; if(v==u) continue; if(vis[u]==vis[v]) return false; if(vis[v]) continue; vis[v]=3-vis[u]; if(!dfs(v)) return false; } return true; } bool judge(int n) { CL(vis,0); REP(i,0,n) if(!vis[i]){ ++vis[i]; if(!dfs(i)) return false; } return true; } int main() { int n,a,b; while(scanf("%d",&n)&&n) { REP(i,0,n) G[i].clear(); while(scanf("%d%d",&a,&b)&&(a+b)) { --a,--b; G[a].push_back(b); G[b].push_back(a); } if(judge(n)) printf("YES\n"); else printf("NO\n"); } return 0; }