标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1141 Accepted Submission(s): 414
大意:有n个人,现在告诉你每个人认识哪些人,问你能不能把这些人分成两个集合,使每个集合中的人之间都相互认识(认识不可传递)
思路:用二分图来做:(晚上再写)
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 105; 8 int G[maxn][maxn]; 9 vector<int> mat[maxn]; 10 int color[maxn]; 11 12 bool is_bi(int u) { 13 for(int i = 0; i < mat[u].size(); i++) { 14 int v = mat[u][i]; 15 if(color[v] == color[u]) return false; 16 if(color[v] == 0) { 17 color[v] = 3 - color[u]; 18 if(!is_bi(v)) return false; 19 } 20 } 21 return true; 22 } 23 24 bool solve(int n) { 25 for(int i = 1; i <= n; i++) { 26 memset(color, 0, sizeof(color)); 27 color[i] = 1; 28 if(!is_bi(i)) return false; 29 } 30 return true; 31 } 32 33 int main() { 34 int n; 35 int id; 36 while(EOF != scanf("%d",&n)) { 37 memset(G, 0, sizeof(G)); 38 memset(mat, 0, sizeof(mat)); 39 for(int i = 1; i <= n; i++) { 40 mat[i].clear(); 41 while(scanf("%d",&id) && id) { 42 G[i][id] = 1; 43 } 44 } 45 for(int i = 1; i <= n; i++) { 46 for(int j = 1; j <= n; j++) { 47 if(i == j) continue; 48 if(G[i][j] + G[j][i] != 2) { 49 // printf("%d %d\n",i, j); 50 mat[i].push_back(j); 51 mat[j].push_back(i); 52 } 53 } 54 } 55 if(solve(n)) puts("YES"); 56 else puts("NO"); 57 } 58 return 0; 59 }
HDU4751Divide Groups【判断二分图】,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/zhanzhao/p/3899028.html