标签:poj
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 27624 | Accepted: 8979 |
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!
好吧。。这道题。。思路不是自己的。。Orz。。
带关系的并查集。。
题意:有n个人,给你m对关系,问有没有同性恋的。
本题需要做的是统计输入的数据是否有相同的根节点,有的话就是违法的,结果也就出来了,没有相同根节点的话,得分别处理
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath>
using namespace std;
#define f1(i, n) for(int i=0; i<n; i++)
#define f2(i, n) for(int i=1; i<=n; i++)
#define f3(i, n) for(int i=n; i>=1; i--)
#define f4(i, n) for(int i=1; i<n; i++)
#define M 10005
int f[M];
int sex[M];
int flag;
int find (int x) //并查集的find
{
return f[x] == x ? x : f[x] = find(f[x]);
}
void make(int a, int b)
{
int x = find(a);
int y = find(b);
if(x!=y)
f[y] = x;
}
int main()
{
int t;
int n, m;
int a, b;
scanf("%d", &t);
f2(cas, t)
{
flag = 0;
memset(sex, 0, sizeof(sex));
scanf("%d%d", &n, &m);
f2(i, n)
f[i] = i; //初始化, 因为题目数据, 所以是从1开始。
f1(i, m)
{
scanf("%d%d", &a, &b);
if(flag)
continue;
if(find(a) == find(b))
{
flag = 1;
continue;
}
if(sex[a]==0)
sex[a] = b; //说明sex[i]与i是异性
else
make(sex[a], b); //与a是异性,那么与b就是同性。。合并
if(sex[b]==0)
sex[b] = a;
else
make(sex[b], a);
}
printf("Scenario #%d:\n", cas);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}
POJ 2492 || HDU 1829:A Bug's Life(并查集),布布扣,bubuko.com
POJ 2492 || HDU 1829:A Bug's Life(并查集)
标签:poj
原文地址:http://blog.csdn.net/u013487051/article/details/37771783