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

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

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int fa[MAXN];
int pic[MAXN][MAXN];
int vis[MAXN];
int ans[MAXN];
int cnt;
int n;
int Find(int v){
return fa[v] = (fa[v] == v) ? v : Find(fa[v]);
}
void dfs(int cur){
if(ans[cur] == n){
++cnt;
for(int i = ; i <= cur; ++i){
if(i != ) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
else{
for(int i = ; i <= ; ++i){
if(pic[ans[cur]][i] && !vis[i]){
vis[i] = ;
ans[cur + ] = i;
dfs(cur + );
vis[i] = ;
}
}
}
}
int main(){
int kase = ;
while(scanf("%d", &n) == ){
int x, y;
memset(pic, , sizeof pic);
memset(vis, , sizeof vis);
memset(ans, , sizeof ans);
cnt = ;
for(int i = ; i < MAXN; ++i){
fa[i] = i;
}
while(scanf("%d%d", &x, &y) == ){
if(!x && !y) break;
pic[x][y] = ;
pic[y][x] = ;
int tx = Find(x);
int ty = Find(y);
if(tx < ty) fa[ty] = tx;
else if(tx > ty) fa[tx] = ty;
}
printf("CASE %d:\n", ++kase);
if(Find(n) != ){
printf("There are 0 routes from the firestation to streetcorner %d.\n", n);
continue;
}
vis[] = ;
ans[] = ;
dfs();
printf("There are %d routes from the firestation to streetcorner %d.\n", cnt, n);
}
return ;
}

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. php如何获取本地手机号

    <?php function inquiry_number_infor($phonenumber) /* *传入手机号码,通过API的到xml格式数据,对xml进一步解析,最后返回相应的号码信息 ...

  2. Java Networking Related (Java Examples in a Nutshell 3rd Edition)

    Examples to: Use URL class to parse URLs and download the network resources specified by a URL Use U ...

  3. ubuntu中安装myeclipse提示Insufficient Memory解决方法

    经过查看资料发现出现这个问题的原因是因为计算机中swap分区的内存不足,或者没有创建swap分区,google中http://www.bkjia.com/webzh/1003601.html提供了一种 ...

  4. java基本输入型数据Scanner

    import java.util.Scanner; public class Example2_3 { public static void main (String args[ ]){ System ...

  5. 5--OC--构造方法

    //  Created by Stephen on 16/3/2.//  Copyright © 2016年 Stephen. All rights reserved.//// 回顾上一章节Perso ...

  6. 关于c中的inline

    在c中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间是有限的 ...

  7. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  8. nodejs实践-代码组织

    nodejs实践-代码组织 laiqun@msn.cn Contents 1. 代码组织 1. 代码组织 更新版本 npm install -g n n latest 项目文件组织 MVC 前后端代码 ...

  9. JSP内置对象--session对象(getId(),getCreationTime(),getLastAccessedTime(),isNew(),invalidate(),setAttribute(),getAttribute())

    session对象是javax.servlet.http.HttpSession接口的实例,但是不像HttpServletRequest或HttpServletResponse一样,有父接口,他没有父 ...

  10. 关于Jsp页面在ww:iterator 标签里面判断的写法是可以直接写数组里面的变量的

    因为上面已经遍历了,所以可以直接写变量名