标签:
Description
Input
Output
Sample Input
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
3 4
Sample Output
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
Hint
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=2005; 7 int father[maxn]; 8 int other[maxn]; 9 int rank[maxn]; 10 int m,n; 11 12 void init(){ 13 for(int i=0;i<=n;i++){ 14 father[i]=i; 15 rank[i]=0; 16 other[i]=0; 17 } 18 } 19 20 int get_father(int x){ 21 if(x!=father[x]) 22 father[x]=get_father(father[x]); 23 return father[x]; 24 } 25 26 void Union(int x,int y){ 27 int t1=get_father(x); 28 int t2=get_father(y); 29 if(rank[t1]>rank[t2]) 30 father[t2]=t1; 31 else{ 32 father[t1]=t2; 33 if(rank[t1]==rank[t2]) 34 rank[t2]++; 35 } 36 } 37 38 int main(){ 39 int t; 40 scanf("%d",&t); 41 int cnt=1; 42 while(t--){ 43 bool flag=false; 44 scanf("%d%d",&n,&m); 45 init(); 46 int x,y; 47 for(int i=1;i<=m;i++){ 48 scanf("%d%d",&x,&y); 49 int t1=get_father(x); 50 int t2=get_father(y); 51 if(flag) 52 continue; 53 if(t1==t2){ 54 flag=true; 55 continue; 56 } 57 if(other[x]){ 58 Union(other[x],y); 59 } 60 else 61 other[x]=y; 62 if(other[y]) 63 Union(other[y],x); 64 else 65 other[y]=x; 66 67 } 68 69 printf("Scenario #%d:\n",cnt++); 70 if(flag) 71 72 printf("Suspicious bugs found!\n"); 73 else 74 printf("No suspicious bugs found!\n"); 75 puts(""); 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/13224ACMer/p/5013760.html