心好痛,本来懒得写,想直接套自己写的好并查集的模板的,结果发现手贱中午被自己删了,并没有软件恢复的了,都怪自己手贱,心好痛啊啊啊啊那么久写的代码啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊。只不过今天Bestcoder打的不错,终于上分了。乐极生悲啊啊啊啊。其实类似食物链,比食物链简单,属于并查集的扩展,上代码。
#include"iostream" using namespace std; const int Max = 2005; int n, m; int parent[Max], opp[Max]; void make_set() { for(int x = 1; x <= n; x ++) { parent[x] = x; opp[x] = 0; } } int find_set(int x) { if(x != parent[x]) parent[x] = find_set(parent[x]); return parent[x]; } void union_set(int x, int y) { x = find_set(x); y = find_set(y); if(x == y) return; parent[y] = x; } int main() { int t, i, x, y; scanf("%d", &t); for(i = 1; i <= t; i ++) { scanf("%d %d", &n, &m); make_set(); bool flag = false; while(m--) { scanf("%d %d", &x, &y); if(flag) continue; if(find_set(x) == find_set(y)) { // 若x,y同在一个集合,则证明有同性的可疑。 flag = true; } if(opp[x] == 0 && opp[y] == 0) { opp[x] = y;//表明y是x的异性 opp[y] = x; } else if(opp[x] == 0) { opp[x] = y; union_set(x, opp[y]); } else if(opp[y] == 0) { opp[y] = x; union_set(y, opp[x]); } else { union_set(x, opp[y]); union_set(y, opp[x]); } } printf("Scenario #%d:\n", i); if(flag) printf("Suspicious bugs found!\n\n"); else printf("No suspicious bugs found!\n\n"); } return 0; }