您的位置:首页 > Web前端

HDU1829 A Bug's Life(种类并查集)

2017-04-13 21:39 363 查看
题目:


A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 14999    Accepted Submission(s): 4877


Problem Description

Background 

Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy
to identify, because numbers were printed on their backs. 

Problem 

Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

 

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each
interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

 

Output

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual
behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

 

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!

HintHuge input,scanf is recommended.

 

Source

TUD Programming Contest 2005, Darmstadt, Germany

 

Recommend

linle   |   We have carefully selected several similar problems for you:  1558 1811 1198 1856 1325 

 

Statistic | Submit | Discuss | Note
思路:

先是有t组数据,然后有一个n和m,n代表几个点,m代表有几行数据,然后每行数据用两个数来描述证明这两个数是异性,最后问我们他给的这些数里面存不存在同性恋,以第一组样例来说明

2(两组数据)

3 3(3个人,3行数)

1 2(1,2性别不一样)

2 3(2,3性别不一样)

1 3(因为从前面可以推出1和3性别一样,所以这里矛盾了,所以这个数据就是错的,所以要输出Suspicious bugs found!)

这道题我们用种类并查集来做,用rank[i]来记录当前点的关系,rank[i]表示i与根节点的关系,0为同性,1为异性

代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define N 2020
#define M 1000000+10
#define ll long long
using namespace std;
ll pre
,rank1
,n,k;
ll flag=0;
void init()
{
flag=0;
for(ll i=0; i<=n; i++)
{
pre[i]=i;
rank1[i]=0;
}
}
ll find(ll x)
{
if(x==pre[x])
return pre[x];
else
{
ll t=find(pre[x]);
rank1[x]=(rank1[pre[x]]+rank1[x])&1;
pre[x]=t;
return pre[x];
}
}
void mix(ll x,ll y)
{
ll fx=find(x);
ll fy=find(y);
if(fx==fy)
{
if(rank1[x]==rank1[y])
flag=1;
return ;
}
pre[fx]=fy;
rank1[fx]=(rank1[x]+rank1[y]+1)&1;
}
int main()
{
ll t,q=1,a,b;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&k);
init();
for(ll i=0; i<k; i++)
{
scanf("%lld%lld",&a,&b);
mix(a,b);
}
if(flag)
printf("Scenario #%lld:\nSuspicious bugs found!\n\n",q++);
else
printf("Scenario #%lld:\nNo suspicious bugs found!\n\n",q++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: