题目链接: hdu 5971 Wrestling Match
题意:N个选手,M场比赛,已知x个好人,y个坏人,问能否将选手划分成好人和坏人两个阵营,保证每场比赛必有一个好人和一个坏人参加。
题解:dfs染色。听说题意模糊?从样例来看,第一个例子应该是因为2可能是好人可能是坏人,所以不确定就NO了...
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 #define CLR(a,b) memset((a),(b),sizeof((a))) 8 using namespace std; 9 typedef long long ll; 10 const int N = 1005; 11 const int inf = 0x3f3f3f3f; 12 int n, m , x, y, flag; 13 vector<int> g[N]; 14 int vis[N]; 15 void dfs(int u) { 16 int f = 0; 17 if(vis[u]==-1) {vis[u] = 0;f = 1;} 18 for(int i = 0; i < g[u].size(); ++i) { 19 int v = g[u][i]; 20 if(vis[v]==vis[u] && f==1) {f=2; vis[u] = 1-vis[v];} 21 if(vis[v]==vis[u]) flag = 0; 22 if(vis[v]==-1) {vis[v] = 1-vis[u]; dfs(v);} 23 } 24 } 25 int main() { 26 int i, j, t, u, v; 27 while(~scanf("%d%d%d%d", &n, &m, &x, &y)) { 28 for(i = 1; i <= n; ++i) g[i].clear(); 29 CLR(vis, -1); flag = 1; 30 while(m--) { 31 scanf("%d%d", &u, &v); 32 g[u].push_back(v); 33 g[v].push_back(u); 34 } 35 while(x--) { 36 scanf("%d", &u); 37 vis[u] = 0; 38 } 39 while(y--) { 40 scanf("%d", &v); 41 vis[v] = 1; 42 } 43 for(i = 1; i <= n; ++i){ 44 if(g[i].size()) dfs(i); 45 } 46 for(i = 1; i <= n; ++i)if(vis[i]==-1){flag = 0;break;} 47 if(flag) puts("YES"); 48 else puts("NO"); 49 } 50 return 0; 51 }