标签:
Description
Input
Output
Sample Input
Sample Output
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 6 using namespace std; 7 8 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 9 //int main(int argc, char** argv) 10 11 #define N 1005 12 13 int n, m, flag, ans, x, y; 14 int maps[N][N], vis[N], used[N], c[N]; 15 16 int found(int u) 17 { 18 for(int i = 1; i <= n; i++) 19 { 20 if(! vis[i] && maps[u][i]) 21 { 22 vis[i] = 1; 23 if(!used[i] || found(used[i])) 24 { 25 used[i] = u; 26 return true; 27 } 28 } 29 } 30 return false; 31 } 32 33 void dfs(int u, int k) //所谓染色 34 { 35 c[u] = k; 36 for(int i = 1; i <= n; i++) 37 { 38 if(maps[u][i] && !c[i])
41 dfs(i, -k); 42 } 43 } 44 45 46 int main() 47 { 48 while(scanf("%d%d", &n, &m) != EOF) 49 { 50 memset(used, 0, sizeof(used)); 51 memset(maps, 0, sizeof(maps)); 52 memset(c, 0, sizeof(c)); 53 54 ans = flag = 0; 55 56 while(m--) 57 { 58 scanf("%d%d", &x, &y); 59 maps[x][y] = 1; 60 vis[x] = vis[y] = true; 61 } 62 63 for(int i = 1; i <= n; i++) 64 { 65 if(!vis[i]) 66 continue; 67 else if(!c[i]) 68 dfs(i, 1); 69 } 70 for(int i = 1; i <= n; i++) 71 { 72 for(int j = 1; j <= n; j++) 73 { 74 if(maps[i][j] && c[i]+c[j]) //一个集合里有认识的人了,flag = 1 75 flag = 1; 76 } 77 } 78 if(flag) 79 printf("No\n"); 80 else 81 { 82 for(int i = 1; i <= n; i++) 83 { 84 memset(vis, 0, sizeof(vis)); 85 if(found(i) && c[i]) 86 ans++; 87 } 88 printf("%d\n", ans); 89 } 90 } 91 return 0; 92 }
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4722232.html