【题目描述】
给出几对虫子交配,问是否有同性恋。
【解题思路】
并查集变形,把每条虫的所有对象合并
int p[10001];
int rank[10001];
int other[10001];/* 每个虫的对象 */
void make_set(int x) {
p[x] = x;
rank[x] = 0;
other[x] = -1;/* 初始化每个虫的对象 */
}
int find_set(int x) {
if (x != p[x])
p[x] = find_set(p[x]);
return p[x];
}
void link(int x, int y) {
if (rank[x] > rank[y]) p[y] = x;
else {
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
}
void union_set(int x, int y) {
link(find_set(x), find_set(y));
}
int ans[10005];
int main()
{
int n;
cin>>n;
int xx = 1;
while (n--) {
int a, b;
scanf("%d%d", &a, &b);
int i, j;
for (i = 1; i <= a; ++i) {
ans[i] = -1;
make_set(i);
}
int flag = 0;
for (i = 0; i < b; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if (find_set(x) == find_set(y))/* 对象是否一样 */
flag = 1;
else {
if (other[x] != -1)
union_set(other[x], y);/* 合并x的对象 */
else other[x] = y;
if (other[y] != -1)
union_set(other[y], x);/* 合并y的对象 */
else other[y] = x;
}
}
printf("Scenario #%d:\n", xx++);
printf("%s\n\n", flag == 0 ? "No suspicious bugs found!" : "Suspicious bugs found!");
}
return 0;
}