首页 > 代码库 > ACM1829并查集
ACM1829并查集
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.
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
23 31 22 31 34 21 23 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。