题意:输入着火点n,求结点1到结点n的所有路径,按字典序输出,要求结点不能重复经过。

分析:用并查集事先判断结点1是否可以到达结点k,否则会超时。dfs即可。

  1. #pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cctype>
  6. #include<cmath>
  7. #include<iostream>
  8. #include<sstream>
  9. #include<iterator>
  10. #include<algorithm>
  11. #include<string>
  12. #include<vector>
  13. #include<set>
  14. #include<map>
  15. #include<stack>
  16. #include<deque>
  17. #include<queue>
  18. #include<list>
  19. #define Min(a, b) ((a < b) ? a : b)
  20. #define Max(a, b) ((a < b) ? b : a)
  21. typedef long long ll;
  22. typedef unsigned long long llu;
  23. const int INT_INF = 0x3f3f3f3f;
  24. const int INT_M_INF = 0x7f7f7f7f;
  25. const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
  26. const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  27. const int dr[] = {, , -, , -, -, , };
  28. const int dc[] = {-, , , , -, , -, };
  29. const int MOD = 1e9 + ;
  30. const double pi = acos(-1.0);
  31. const double eps = 1e-;
  32. const int MAXN = + ;
  33. const int MAXT = + ;
  34. using namespace std;
  35. int fa[MAXN];
  36. int pic[MAXN][MAXN];
  37. int vis[MAXN];
  38. int ans[MAXN];
  39. int cnt;
  40. int n;
  41. int Find(int v){
  42. return fa[v] = (fa[v] == v) ? v : Find(fa[v]);
  43. }
  44. void dfs(int cur){
  45. if(ans[cur] == n){
  46. ++cnt;
  47. for(int i = ; i <= cur; ++i){
  48. if(i != ) printf(" ");
  49. printf("%d", ans[i]);
  50. }
  51. printf("\n");
  52. }
  53. else{
  54. for(int i = ; i <= ; ++i){
  55. if(pic[ans[cur]][i] && !vis[i]){
  56. vis[i] = ;
  57. ans[cur + ] = i;
  58. dfs(cur + );
  59. vis[i] = ;
  60. }
  61. }
  62. }
  63. }
  64. int main(){
  65. int kase = ;
  66. while(scanf("%d", &n) == ){
  67. int x, y;
  68. memset(pic, , sizeof pic);
  69. memset(vis, , sizeof vis);
  70. memset(ans, , sizeof ans);
  71. cnt = ;
  72. for(int i = ; i < MAXN; ++i){
  73. fa[i] = i;
  74. }
  75. while(scanf("%d%d", &x, &y) == ){
  76. if(!x && !y) break;
  77. pic[x][y] = ;
  78. pic[y][x] = ;
  79. int tx = Find(x);
  80. int ty = Find(y);
  81. if(tx < ty) fa[ty] = tx;
  82. else if(tx > ty) fa[tx] = ty;
  83. }
  84. printf("CASE %d:\n", ++kase);
  85. if(Find(n) != ){
  86. printf("There are 0 routes from the firestation to streetcorner %d.\n", n);
  87. continue;
  88. }
  89. vis[] = ;
  90. ans[] = ;
  91. dfs();
  92. printf("There are %d routes from the firestation to streetcorner %d.\n", cnt, n);
  93. }
  94. return ;
  95. }

UVA - 208 Firetruck(消防车)(并查集+回溯)的更多相关文章

  1. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

  2. UVa 208 - Firetruck 回溯+剪枝 数据

    题意:构造出一张图,给出一个点,字典序输出所有从1到该点的路径. 裸搜会超时的题目,其实题目的数据特地设计得让图稠密但起点和终点却不相连,所以直接搜索过去会超时. 只要判断下起点和终点能不能相连就行了 ...

  3. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  4. UVA 12232 Exclusive-OR(并查集+思想)

    题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...

  5. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  6. UVA 10158 War(并查集)

    //思路详见课本 P 214 页 思路:直接用并查集,set [ k ]  存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k  的敌人 所在集合的代表元素. #include< ...

  7. UVA - 11987 Almost Union-Find 并查集的删除

    Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...

  8. uva 6910 - Cutting Tree 并查集的删边操作,逆序

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. UVA 11987 Almost Union-Find 并查集单点修改

                                     Almost Union-Find I hope you know the beautiful Union-Find structur ...

随机推荐

  1. Tiny210编译和烧写u-boot步骤

    当有多个交叉编译器是,不方便设置环境变量时,可以在编译命令中指定交叉编译器,具体如下: make ARCH=arm CROSS_COMPILE=/opt/FriendlyARM/toolschain/ ...

  2. python 第三方库下载

    C:\Python27\Scripts 路径下: easy_install.exe: C:\Python27\Scripts>easy_install.exe pycrypto pip.exe: ...

  3. Node.js学习 - Function

    Node.js函数和JavaScript类似 function say(word) { console.log(word); } function execute(someFunction, valu ...

  4. OpenCv的Java,C++开发环境配置

    1.OpenCV 下载及安装配置 opencv的下载地址:http://opencv.org/downloads.html 最新版本:opencv3.0.0 注意:支持的visual studio20 ...

  5. 错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 的处理方法

  6. hdu_3886_Final Kichiku “Lanlanshu”(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3886 题意:这题的题意有点晦涩难懂,大概意思就是给你一个区间,让你找一些满足递增递减条件的数,举个列: ...

  7. Django之路:QuerySet API,后台和表单

    一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...

  8. (转)hadoop三个配置文件的参数含义说明

     hadoop三个配置文件的参数含义说明     1       获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配 ...

  9. mr本地运行的几种模式

    MR程序的几种提交运行模式 本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路 ...

  10. 序列化为XML

    java类序列化成xml 方法[转] 今天看了下JAVA序列化.还是一知半解.怎么也没有弄明白,怎么序列化成XML文件.处入半解状态.在网上找了很多,大部分是理论上的.没有实际的例子.功夫不负有心人, ...