Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 33833 | Accepted: 11078 |
Description
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
Output
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
Source
带偏移量的并查集。
两个集合并时,要注意处理dis数组,使点之间的关系不出错
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 const int mxn=20000; 8 int fa[mxn]; 9 int dis[mxn]; 10 int n,m; 11 bool flag; 12 int find(int x){ 13 if(fa[x]==x){ 14 return x; 15 } 16 int newfx=find(fa[x]); 17 dis[x]=(dis[x]+dis[fa[x]])%2; 18 return fa[x]=newfx; 19 } 20 bool pd(int a,int b){ 21 int u=find(a),v=find(b); 22 if(u==v){ 23 if(dis[a]!=(dis[b])^1){ 24 flag=1; 25 return 0; 26 } 27 } 28 else{ 29 fa[u]=v; 30 dis[u]=((dis[a]-dis[b])^1); 31 } 32 return 1; 33 } 34 void init(int x){ 35 for(int i=1;i<=x;i++)fa[i]=i,dis[i]=0; 36 } 37 int main(){ 38 int T; 39 scanf("%d",&T); 40 for(int ro=1;ro<=T;ro++){ 41 printf("Scenario #%d:\n",ro); 42 scanf("%d%d",&n,&m); 43 init(n); 44 int i,j; 45 int x,y; 46 flag=0; 47 for(i=1;i<=m;i++){ 48 scanf("%d%d",&x,&y); 49 pd(x,y); 50 } 51 if(flag)printf("Suspicious bugs found!\n"); 52 else printf("No suspicious bugs found!\n"); 53 printf("\n"); 54 } 55 return 0; 56 }