题意:给出n个虫子的m种关系,这种关系表示两个虫子性别不同,问有没有产生歧义。
解法:并查集+向量偏移。跟POJ1173几乎一样。
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #define LL long long using namespace std; int father[100005], delta[100005]; void init() { memset(delta, 0, sizeof delta); for(int i = 0; i < 100005; i++) father[i] = i; } int Find(int a) { if(father[a] != a) { int tmp = Find(father[a]); delta[a] = (delta[a] + delta[father[a]]) % 2; father[a] = tmp; } return father[a]; } int main() { int T; scanf("%d", &T); int cse = 1; while(T--) { init(); int n, m; scanf("%d%d", &n, &m); bool ans = 0; while(m--) { int x, y; scanf("%d%d", &x, &y); int c = Find(x), d = Find(y); if(c != d) { father[c] = d; delta[c] = (delta[y] - delta[x] + 1) % 2; continue; } if(delta[x] == delta[y]) ans = 1; } printf("Scenario #%d: ", cse++); if(ans) puts("Suspicious bugs found! "); else puts("No suspicious bugs found! "); } return 0; }