码迷,mamicode.com
首页 > 其他好文 > 详细

ACM1829并查集

时间:2014-07-28 11:26:50      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   strong   io   for   

A Bug‘s Life

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!
Hint
Huge input,scanf is recommended.
 1 #include<iostream>
 2 using namespace std;
 3 struct Pair
 4 {
 5     int from,conflict;
 6 };
 7 Pair *record;
 8 int find(int x)
 9 {
10     if(x==record[x].from)
11     return x;
12     return record[x].from=find(record[x].from);
13 }
14 void link(int a,int b)
15 {
16     int fa=find(a);
17     int fb=find(b);
18     if(fa!=fb)
19     record[fb].from=fa;
20 }
21 int main()
22 {
23     int t;
24     int cnt=1;
25     scanf("%d",&t);
26     while(t--)
27     {
28         int n,m;
29         bool flag=false;
30         scanf("%d %d",&n,&m);
31         record=new Pair[n+1];
32         for(int i=1;i<=n;i++)
33         {
34             record[i].from=i;
35             record[i].conflict=0;
36         }
37         int a,b;
38         for(int i=1;i<=m;i++)
39         {
40             scanf("%d %d",&a,&b);
41             int fa=find(a);
42             int fb=find(b);
43             if(fa==fb)flag=true;
44             if(record[a].conflict!=0)
45             {
46                 link(b,record[a].conflict);
47             }
48             if(record[b].conflict!=0)
49             {
50                 link(a,record[b].conflict);
51             }
52             record[a].conflict=b;
53             record[b].conflict=a;
54         }
55         cout<<"Scenario #"<<cnt<<":\n";
56         if(flag)cout<<"Suspicious bugs found!\n";
57         else cout<<"No suspicious bugs found!\n";
58         cout<<endl;
59         cnt++;
60     }
61     return 0;
62 }

 

ACM1829并查集,布布扣,bubuko.com

ACM1829并查集

标签:des   style   blog   color   os   strong   io   for   

原文地址:http://www.cnblogs.com/sytu/p/3872234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!