题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272
注意问题:
1、不能成环,即每次输入的两个数的根节点不能相同;
2、只有一个迷宫,即根节点数目唯一;
3、注意当只输入“0 0” 时要输出"Yes";
4、状态压缩用递归回栈溢出。
参考代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include<stdio.h> int fa[100001]; int find(int u) { int s; s=u; while (fa[u]!=u)u=fa[u]; while (fa[s]!=s){fa[s]=u;s=fa[s];} return u; } int main() { int u,v,x,y,n,m,i,k; bool bo; scanf("%d%d",&u,&v); while (u!=-1) { if (u!=0) { for (i=1;i<=100000;i++) fa[i]=i; bo=true; while (u) { x=find(u);y=find(v); if (x==y) {bo=false;} else fa[x]=y; scanf("%d%d",&u,&v); } if (!bo) printf("Non"); int j; if(bo) { for (i=1;i<=100000;i++) if (find(i)!=i) break; for (j=i+1;j<=100000;j++) if ((find(j)!=j)&&(find(j)!=find(i))) { printf("Non"); bo=false; break; } } if (bo) printf("Yesn"); } else printf("Yesn"); scanf("%d%d",&u,&v); } return 0; }<strong> </strong>
同类题请参考:http://blog.csdn.net/yzj577/article/category/2432227
最后
以上就是坦率学姐最近收集整理的关于HDU1272 小希的迷宫 (并查集)的全部内容,更多相关HDU1272内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复