标签:des style blog color os strong io for
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28211 | Accepted: 9177 |
Description
Input
Output
Sample Input
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
Sample Output
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
Hint
1 #include <cstdio> 2 #include <cstring> 3 4 const int LEN = 2020; 5 6 int uset[LEN]; 7 int opp[LEN]; //代表每个虫子的配对异性 8 int rank[LEN]; 9 int n; 10 11 void makeset() //初始化并查集 12 { 13 for(int i = 0; i <= n; i++) 14 uset[i] = i; 15 memset(opp, 0, sizeof(opp)); 16 memset(rank, 0, sizeof(rank)); 17 } 18 19 int findset(int x) 20 { 21 if (x == uset[x]) 22 return x; 23 else 24 uset[x] = findset(uset[x]); 25 return uset[x]; 26 } 27 28 void unionset(int x, int y) 29 { 30 if ((x = findset(x)) == (y = findset(y))) 31 return; 32 if (rank[x] > rank[y]) //按秩合并 33 uset[y] = x; 34 else{ 35 uset[x] = y; 36 if (rank[x] == rank[y]) 37 ++rank[y]; 38 } 39 } 40 41 int main() 42 { 43 int T; 44 scanf("%d", &T); 45 for(int cnt = 1; cnt <= T; cnt++){ 46 int m; 47 scanf("%d %d", &n, &m); 48 makeset(); 49 int f = 0; 50 for(int i = 0; i < m; i++){ 51 int x, y; 52 scanf("%d %d", &x, &y); 53 if (f) 54 continue; 55 if (findset(x) == findset(y)){ //如果这两个虫子在同一个集合里找到 就是同性恋 56 f = 1; 57 continue; 58 } 59 if (opp[x] == 0 && opp[y] == 0){ //代表两个虫子都没被分类过,储存下每个虫子的异性 60 opp[x] = y; 61 opp[y] = x; 62 } 63 else if (opp[x] == 0){ //如果x没被分类过,将它的异性记录为y,并将它加入y的异性所在的集合 64 opp[x] = y; 65 unionset(x, opp[y]); 66 } 67 else if (opp[y] == 0){ //同上 68 opp[y] = x; 69 unionset(y, opp[x]); 70 } 71 else{ //否者合并x也opp[y]所在的集合以及y与opp[x]所在的集合 72 unionset(x, opp[y]); 73 74 unionset(y, opp[x]); 75 } 76 } 77 printf("Scenario #%d:\n", cnt); 78 if (f) 79 printf("Suspicious bugs found!\n"); 80 else 81 printf("No suspicious bugs found!\n"); 82 printf("\n"); 83 } 84 return 0; 85 }
【POJ】2492 A bug's life ——种类并查集,布布扣,bubuko.com
【POJ】2492 A bug's life ——种类并查集
标签:des style blog color os strong io for
原文地址:http://www.cnblogs.com/kevince/p/3891379.html