您的位置:首页 > 其它

hdu 1829(并查集)

2017-07-14 20:39 176 查看
题意:两两配偶,其中是一男一女,问会不会出现冲突。

思路:两个并查集,1 ~ n代表男,n + 1 ~ n * 2代表女。

          当一个人同时为男或者同时为女的时候冲突产生。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int maxn = 2000 + 10;
#define clr(x,y) memset(x,y,sizeof x)
int fa[maxn * 2];
int finds(int x)
{
return fa[x] == x ? x : (fa[x] = finds(fa[x]));
}
void unions(int x,int y)
{
int xx = finds(x),yy = finds(y);
if(xx != yy)
fa[xx] = yy;
}
int main()
{
int T;
scanf("%d",&T);
for(int ii = 1; ii <= T ; ii ++)
{
bool ans = true;
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n * 2;i ++)
fa[i] = i;
while(m --)
{
int x,y;
scanf("%d%d",&x,&y);
unions(x,y + n);
unions(x + n,y);
}
printf("Scenario #%d:\n",ii);
for(int i = 1; i <= n; i ++)
{
if(finds(i) == finds(i + n))
{
ans = false;
break;
}
}
if(ans)
puts("No suspicious bugs found!\n");
else puts("Suspicious bugs found!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: