HDOJ 1272 小希的迷宫

  教训是深刻的,读题不认真,所以只考虑“是否只有一条线”,于是有了WA的解答。认真读题之后幡然醒悟,于是有了AC的解答,审题要认真啊~~~~(>_<)~~~~ 

WA解答:

View Code
 1 //#include <fstream>   
2 #include <iostream>
3 using namespace std;
4 unsigned point[100001];
5
6 unsigned find(unsigned value)
7 {
8 int temp=value;
9 while(value!=point[value])
10 value=point[value];
11 int j;
12 while (temp!=value)
13 {
14 j= point[temp];
15 point[temp] =value;
16 temp=j;
17 }
18 return value;
19 }
20 int merge(unsigned xvalue,unsigned yvalue)
21 {
22 unsigned x=find(xvalue);
23 unsigned y=find(yvalue);
24 if(x!=y)
25 point[x]=y;
26 else
27 return 1;
28 return 0;
29 }
30
31 int main()
32 {
33 //ifstream cin("小希的迷宫.txt");
34 int FPoint,SPoint;
35 while (cin>>FPoint>>SPoint)
36 {
37 if(FPoint==-1&&SPoint==-1)
38 break;
39 for(int j=0;j<100001;j++)
40 point[j]=j;
41 int flag=0;
42 while (FPoint&&SPoint)
43 {
44 if(!flag)
45 flag=merge(FPoint,SPoint);
46 cin>>FPoint>>SPoint;
47 }
48 if (flag)
49 cout<<"No"<<endl;
50 else
51 cout<<"Yes"<<endl;
52 }
53 return 0;
54 }

AC解答:

View Code
 1 //#include <fstream>   
2 #include <iostream>
3 using namespace std;
4 unsigned point[100001];
5 bool mark[100001];
6 unsigned find(unsigned value)
7 {
8 int temp=value;
9 while(value!=point[value])
10 value=point[value];
11 int j;
12 while (temp!=value)
13 {
14 j= point[temp];
15 point[temp] =value;
16 temp=j;
17 }
18 return value;
19 }
20 bool merge(unsigned xvalue,unsigned yvalue)
21 {
22 unsigned x=find(xvalue);
23 unsigned y=find(yvalue);
24 if(x!=y)
25 point[x]=y;
26 else
27 return 1;
28 return 0;
29 }
30
31 int main()
32 {
33 //ifstream cin("小希的迷宫.txt");
34 int FPoint,SPoint;
35 while (cin>>FPoint>>SPoint)
36 {
37 if(FPoint==-1&&SPoint==-1)
38 break;
39 memset(mark,0,sizeof(mark));
40 for(int j=0;j<100001;j++)
41 point[j]=j;
42 bool flag=0;
43 while (FPoint&&SPoint)
44 {
45 if(!flag)
46 flag=merge(FPoint,SPoint);
47 mark[FPoint]=true;
48 mark[SPoint]=true;
49 cin>>FPoint>>SPoint;
50 }
51
52 if (flag)
53 cout<<"No"<<endl;
54 else
55 {
56 int count=-1;
57 for(int j=1;j<100001;j++)
58 if(j==point[j]&&mark[j])
59 count++;
60 if (count==0||count==-1)
61 cout<<"Yes"<<endl;
62 else
63 cout<<"No"<<endl;
64 }
65
66 }
67 return 0;
68 }




posted on 2011-07-31 22:10  AdaByron  阅读(172)  评论(0编辑  收藏  举报

导航