【二分图判定】hdu3478 Catch
生活随笔
收集整理的這篇文章主要介紹了
【二分图判定】hdu3478 Catch
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
詳細的題解:http://www.docin.com/p-517243379.html
一個圖是二分圖 等價于 其至少有兩個節點且沒有奇環。
二分圖判定的方法:從任意點出發進行一次dfs黑白染色,若某個點之前已經訪問過(vis[v]==1)且color[v]==color[u],則存在奇環。
1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 using namespace std; 5 #define N 100001 6 vector<int>G[N]; 7 typedef vector<int>::iterator ITER; 8 int T,n,m,S,vis[N]; 9 bool col[N]; 10 int x,y; 11 bool dfs(int U,bool now) 12 { 13 vis[U]=1; 14 for(ITER it=G[U].begin();it!=G[U].end();it++) 15 if(!vis[*it]) 16 { 17 col[*it]=(now^1); 18 if(!dfs(*it,now^1)) return 0; 19 } 20 else if(col[*it]==col[U]) return 0; 21 return 1; 22 } 23 int main() 24 { 25 scanf("%d",&T); 26 for(int q=1;q<=T;q++) 27 { 28 printf("Case %d: ",q); 29 memset(vis,0,sizeof(vis)); 30 scanf("%d%d%d",&n,&m,&S); S++; 31 for(int i=1;i<=m;i++) 32 { 33 scanf("%d%d",&x,&y); 34 G[x+1].push_back(y+1); 35 G[y+1].push_back(x+1); 36 } 37 if(!dfs(S,col[S])) puts("YES"); 38 else puts("NO"); 39 if(q==T) break; 40 for(int i=1;i<=n;i++) G[i].clear(); 41 } 42 return 0; 43 }轉載于:https://www.cnblogs.com/autsky-jadek/p/4076944.html
總結
以上是生活随笔為你收集整理的【二分图判定】hdu3478 Catch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学海无涯
- 下一篇: Daily Scrum4 11.6