有n个人,m场摔跤比赛,其中已确定x个好的选手,y个差的选手。
问能否将每个人划分成好的选手或差的选手。
http://acm.hdu.edu.cn/showproblem.php?pid=5971
二分图染色模板题
dfs染色先从已经确定的人开始染色与其相关的人,矛盾就 NO
然后对于不确定的人,枚举染色,如果矛盾就是 NO
如果最后还存在不确定的,那么就是 NO
否则就是 YES
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAX_N=1005; 4 int n,color[MAX_N]; 5 vector<int> G[MAX_N]; 6 bool dfs(int v,int c) 7 { 8 color[v]=c; 9 for(unsigned i=0;i<G[v].size();++i) 10 { 11 if(color[G[v][i]]==c) 12 return false; 13 if(color[G[v][i]]==0&&!dfs(G[v][i],-c)) 14 return false; 15 } 16 return true; 17 } 18 void solve() 19 { 20 for(int i=1;i<=n;++i) 21 if(color[i]==-1) 22 { 23 cout<<"NO\n"; 24 return; 25 } 26 for(int i=1;i<=n;++i) 27 { 28 if(color[i]==2&& !dfs(i,2)) 29 { 30 cout<<"NO\n"; 31 return; 32 } 33 if(color[i]==-2&& !dfs(i,-2)) 34 { 35 cout<<"NO\n"; 36 return; 37 } 38 } 39 for(int i=1;i<=n;++i) 40 if(color[i]==0) 41 { 42 if(!dfs(i,2)) 43 { 44 cout<<"NO\n"; 45 return; 46 } 47 } 48 cout<<"YES\n"; 49 } 50 int main() 51 { 52 ios::sync_with_stdio(false); 53 int m,x,y; 54 while(cin>>n>>m>>x>>y) 55 { 56 int a,b; 57 58 for(int i=1;i<=n;++i)//清空数据 59 G[i].clear(); 60 memset(color,-1,sizeof(color)); 61 62 for(int i=1;i<=m;++i) 63 { 64 cin>>a>>b; 65 color[a]=0; 66 color[b]=0; 67 G[a].push_back(b); 68 G[b].push_back(a); 69 } 70 for(int i=1;i<=x;++i) 71 { 72 cin>>a; 73 color[a]=2; 74 } 75 for(int i=1;i<=y;++i) 76 { 77 cin>>b; 78 color[b]=-2; 79 } 80 solve(); 81 } 82 return 0; 83 }
【2016ACM/ICPC亚洲区沈阳站A】HDU - 5971 Wrestling Match 二分图染色
原文:https://www.cnblogs.com/helloWR/p/10909094.html