hdu1829 参考答案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include
19 using namespace std;
20 const int N=2010;
21 int p[N];
22 int opp[N];
23 int n,m;
24 bool ans;
25 int FindRoot(int x)
26 {
27 int rx=x;
28 while(p[rx]!=0)
29 rx=p[rx];
30 int tx=x;
31 int tmp;
32 while(p[tx]!=0)
33 {
34 tmp=p[tx];
35 p[tx]=rx;
36 tx=tmp;
37 }
38 return rx;
39 }
40 void Merge(int x,int y)
41 {
42 int rx,ry;
43 rx=FindRoot(x);
44 ry=FindRoot(y);
45 if(rx!=ry)
46 p[ry]=rx;
47 }
48 void Solve()
49 {
50 int x,y;
51 ans=true;
52 memset(p,0,sizeof(p));
53 memset(opp,0,sizeof(opp));
54 scanf("%d %d",&n,&m);
55 int rx,ry;
56 for(int i=0;i<m;i++)
57 {
58 scanf("%d %d",&x,&y);
59 if(ans==false)
60 continue;
61 rx=FindRoot(x);
62 ry=FindRoot(y);
63 if(rx==ry)
64 {
65 ans=false;
66 continue;
67 }
68 if(opp[x]==0)
69 opp[x]=y;
70 else
71 Merge(opp[x],y);
72 if(opp[y]==0)
73 opp[y]=x;
74 else
75 Merge(x,opp[y]);
76 }
77 }
78 int main()
79 {
80 int test;
81 scanf("%d",&test);
82 for(int t=1;t<=test;t++)
83 {
84 Solve();
85 printf("Scenario #%d:\n",t);
86 if(ans)
87 printf("No suspicious bugs found!\n\n");
88 else
89 printf("Suspicious bugs found!\n\n");
90
91 }
92 }
hdu1829 参考答案 (2008-11-12 14:19:00由218编辑)