UVA208-Firetruck(并查集+dfs)
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
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)的更多相关文章
- UVA - 208 Firetruck(并查集+dfs)
题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...
- UVa-208 Firetruck (图的DFS)
UVA-208 天道好轮回.UVA饶过谁. 就是一个图的DFS. 不过这个图的边太多,要事先判一下起点和终点是否联通(我喜欢用并查集),否则会TLE. #include <iostream> ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- POJ1291-并查集/dfs
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- hdu6370 并查集+dfs
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- python文件夹copy器(多进程版)
本节的练习的要求如下: 输入要拷贝文件夹的文件名称 读取该文件夹下的所有文件 启动5个进程来拷贝文件夹,将拷贝成功的文件名称放入队列中 主进程中显示文件拷贝的进度 代码如下: import multi ...
- 【Tomcat】上线部署tomcat。常用命令
ps -ef | grep tomcat-web [查询tomact进程]kill -9 pid [结束tomcat进程]/opt/tomcat-web/bin/startup.sh [启动tomca ...
- mysql不能保存中文
进入mysql文件夹,新建(修改) my.ini 文件, 修改编码 内容如下: [mysql] default-character-set=utf8
- laravel5.5通过Migrations修改表 的artisan命令
1,不同表的修改都需要通过命令创建一个文件 2,首先通过artisan创建对应表的一个文件 php artisan make:module:migration abtinvitcard(模块名) al ...
- HTML5效果:Canvas 实现圆形进度条并显示数字百分比
实现效果 1.首先创建html代码 <canvas id="canvas" width="500" height="500" styl ...
- POJ1275 Cashier Employment(差分约束)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9078 Accepted: 3515 Description A sup ...
- 【20190220】JavaScript-知识点整理:对象创建方式、原型、闭包
一.对象创建方式 1. 工厂模式 这种模式抽象了创建具体对象的过程,用函数来封装以特定接口创建对象的细节.存在的问题是无法通过 instanceof 识别一个对象的类型. function creat ...
- 【读书笔记】iOS-库
一,OS X和iOS自带一些标准的C程序库和操作系统相关的特殊性.在MAC和iOS的世界里,静态库采用.a扩展名(静态对象代码库存档),动态加载库采用.dylib扩展名.如果开发人员来自于Linux世 ...
- java内存分配与垃圾回收
JVM的内存分配主要基于两种,堆和栈. 我们来看一下java程序运行时候的内存分配策略: 1):静态存储区(方法区): 2):栈区: 3):堆区: 1):主要存放静态数据,全局static数据和常量. ...
- Android为TV端助力 内存溢出与内存泄露
内存溢出就是软件运行需要的内存,超出了java虚拟机给他分配的可用的最大内存 内存泄露就是在缓存图片文字等等的时候,没有关闭流所导致的内存泄露