HDU - 1829 A Bug‘s Life

https://vjudge.net/problem/HDU-1829
题解:数组倍增,给每个虫子一个异性恋的对象集合,然后每次输入的两个虫子如果不在同一个集合内,就放到对方异性恋集合内,否则无法验证猜想。

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=2010;
int T,n,m;
int p[2*N];
int find(int x)
{
	if(p[x]!=x) p[x]=find(p[x]);
	return p[x];
}
int main()
{
	scanf("%d",&T);
	int cnt=0;
	while(T--)
	{
		scanf("%d %d",&n,&m);
		for(int i=1;i<=2*n;i++) p[i]=i;
		int a,b;
		int f=0;
		while(m--)
		{
			scanf("%d %d",&a,&b);
			int fa=find(a),fb=find(b);
			if(fa==fb)
				f=1;
			else
			{
				int t1=find(a+n),t2=find(b+n);
				p[fa]=t2;
				p[fb]=t1;
			}
		}
		printf("Scenario #%d:\n",++cnt);
		if(f)
			printf("Suspicious bugs found!\n");
		else
			printf("No suspicious bugs found!\n");
		printf("\n");
	}
	return 0;
}

https://blog.csdn.net/hyczms/article/details/45440825

posted @ 2021-08-27 11:24  斯文~  阅读(19)  评论(0)    收藏  举报

你好!