Problem UVA208-Firetruck

Accept:1733  Submit:14538

Time Limit: 3000 mSec

 Problem Description

The Center City fire department collaborates with the transportation department to maintain maps of the city which reflects the current status of the city streets. On any given day, several streets are closed for repairs or construction. Firefighters need to be able to select routes from the firestations to fires that do not use closed streets.
Central City is divided into non-overlapping fire districts, each containing a single firestation. When a fire is reported, a central dispatcher alerts the firestation of the district where the fire is located and gives a list of possible routes from the firestation to the fire. You must write a program that the central dispatcher can use to generate routes from the district firestations to the fires.

 Input

The city has a separate map for each fire district. Streetcorners of each map are identified by positive integers less than 21, with the firestation always on corner #1. The input file contains several test cases representing different fires in different districts.
• The first line of a test case consists of a single integer which is the number of the streetcorner closest to the fire.

• The next several lines consist of pairs of positive integers separated by blanks which are the adjacent streetcorners of open streets. (For example, if the pair 4 7 is on a line in the file, then the street between streetcorners 4 and 7 is open. There are no other streetcorners between 4 and 7 on that section of the street.)

• The final line of each test case consists of a pair of 0’s.

 Output

For each test case, your output must identify the case by number (‘CASE 1:’, ‘CASE 2:’, etc). It must list each route on a separate line, with the streetcorners written in the order in which they appear on the route. And it must give the total number routes from firestation to the fire. Include only routes which do not pass through any streetcorner more than once. (For obvious reasons, the fire department doesn’t want its trucks driving around in circles.) Output from separate cases must appear on separate lines.

 Sample Input

6 1 2 1 3 3 4 3 5 4 6 5 6 2 3 2 4 0 0 4 2 3 3 4 5 1 1 6 7 8 8 9 2 5 5 7 3 1 1 8 4 6 6 9 0 0
 

 Sample Ouput

CASE 1:
1 2 3 4 6
1 2 3 5 6
1 2 4 3 5 6
1 2 4 6
1 3 2 4 6
1 3 4 6
1 3 5 6
There are 7 routes from the firestation to streetcorner 6.
CASE 2:
1 3 2 5 7 8 9 6 4
1 3 4
1 5 2 3 4
1 5 7 8 9 6 4
1 6 4
1 6 9 8 7 5 2 3 4
1 8 7 5 2 3 4
1 8 9 6 4
There are 8 routes from the firestation to streetcorner 4.

题解:水题,寻找路径之前先判断是否连通,并查集可以搞定,搜索时因为要保证字典序,因此先从标号小的开始搜。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = ;

 vector< vector<int> > G(maxn);
int tar,ans;
int pre[maxn];
bool vis[maxn];
int sta[maxn]; int findn(int x){
return x == pre[x] ? x : pre[x] = findn(pre[x]);
} void merge_node(int x,int y){
int fx = findn(x);
int fy = findn(y);
if(fx != fy){
pre[fx] = fy;
}
} void dfs(int u,int pos){
sta[pos] = u;
if(u == tar){
ans++;
printf("%d",sta[]);
for(int i = ;i <= pos;i++){
printf(" %d",sta[i]);
}
printf("\n");
return;
}
for(int i = ;i < G[u].size();i++){
int v = G[u][i];
if(!vis[v]){
vis[v] = true;
dfs(v,pos+);
vis[v] = false;
}
}
} int iCase = ; int main()
{
#ifdef GEH
freopen("helloworld.01.inp","r",stdin);
#endif
while(~scanf("%d",&tar)){
int u,v;
for(int i = ;i < maxn;i++){
G[i].clear();
pre[i] = i;
}
while(~scanf("%d%d",&u,&v) && (u||v)){
G[u].push_back(v);
G[v].push_back(u);
merge_node(u,v);
}
ans = ;
printf("CASE %d:\n",iCase++);
if(findn() != findn(tar)){
printf("There are %d routes from the firestation to streetcorner %d.\n",,tar);
}
else{
for(int i = ;i < maxn;i++){
if(G[i].size()){
sort(G[i].begin(),G[i].end());
}
}
memset(vis,false,sizeof(vis));
vis[] = true;
dfs(,);
printf("There are %d routes from the firestation to streetcorner %d.\n",ans,tar);
}
}
return ;
}

UVA208-Firetruck(并查集+dfs)的更多相关文章

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

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

  2. UVa-208 Firetruck (图的DFS)

    UVA-208 天道好轮回.UVA饶过谁. 就是一个图的DFS. 不过这个图的边太多,要事先判一下起点和终点是否联通(我喜欢用并查集),否则会TLE. #include <iostream> ...

  3. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  4. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  5. POJ1291-并查集/dfs

    并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...

  6. F2 - Spanning Tree with One Fixed Degree - 并查集+DFS

    这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...

  7. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  8. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  9. hdu6370 并查集+dfs

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. js 返回小数点后几位

    function fmoney(s, n) //s:传入的float数字 ,n:希望返回小数点几位 { n = n > 0 && n <= 20 ? n : 2; s = ...

  2. POJ 2484 A Funny Game(智商博弈)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6397   Accepted: 3978 Description Alice ...

  3. BZOJ2655: calc(dp 拉格朗日插值)

    题意 题目链接 Sol 首先不难想到一个dp 设\(f[i][j]\)表示选了\(i\)个严格递增的数最大的数为\(j\)的方案数 转移的时候判断一下最后一个位置是否是\(j\) \[f[i][j] ...

  4. vivo怎么录屏 手机录制屏幕详细教程

    在手机上我们经常可以刷到许多类似于手机游戏之类的屏幕视频我想肯定会有很多人好奇怎么录制的,今天小编所说的便是教大家如何在安卓手机上进行屏幕录像,下面便是关于vivo怎么录屏的具体操作方法,希望能对你们 ...

  5. GDPR 和个人信息保护的小知识

    从2018年5月25日起,欧盟的<通用数据保护条例>(简称 GDPR,General Data Protection Regulation)开始强制施行.这个规范加强了对个人信息的保护,并 ...

  6. 10.Odoo产品分析 (二) – 商业板块(5) –日历(1)

    查看Odoo产品分析系列--目录 日历模板也可以理解为一个日历视图,在分析"销售"模块的日历视图时做过介绍.在这里做一下详细的介绍:  从页面上,它横向分为两个部分,左边的80%显 ...

  7. react 会员登录

    会员登录在我们的好多项目中都有用到,比如在后台管理系统,它的第一步就需要你进行登录,还有在我们常见的京东.淘宝.网易云音乐等一系列的软件上面都需要进行登录. 下面我们直接上代码 fetch(url,{ ...

  8. Node.js学习起步

    Node.js学习: 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台.Node.js是一个事件驱 ...

  9. 全方位理解Android权限之底层实现概览

    0000 这个阶段搞了很多和Android文件权限相关的问题,虽然一知半解,但也算是对Android权限机制有一些自己的理解.遂将这些内容整理出来.因为权限这部分涉及到的内容很多,故将知识分为几块内容 ...

  10. 编程经验点滴----在 Oracle 数据库中保存空字符串

    写程序这么多年,近几天才发现,向 Oracle 数据库表中,保存空字符串 '' ,结果成了 null. 由于数据库数值 null 的比较.判断,与空字符串 '' 存在差异.一不留神,代码中留下了 bu ...