UVA208-Firetruck(并查集+dfs)
Problem UVA208-Firetruck
Accept:1733 Submit:14538
Time Limit: 3000 mSec
 Problem Description
 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
 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
 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 Input
 Sample Ouput
 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 ... 
随机推荐
- Java集合类根接口:Collection 和 Map
			前言 在前文中我们了解了几种常见的数据结构,这些数据结构有着各自的应用场景,并且被广泛的应用于编程语言中,其中,Java中的集合类就是基于这些数据结构为基础. Java的集合类是一些非常实用的工具类, ... 
- RabbitMQ 基本概念总结
			1.ack模式-应答模式 执行一个任务可能需要花费几秒钟,你可能会担心如果一个消费者在执行任务过程中挂掉了.一旦RabbitMQ将消息分发给了消费者,就会从内存中删除.在这种情况下,如果正在执行任务的 ... 
- Advanced redirection features
			here are three types of I/O, which each have their own identifier, called a file descriptor: standar ... 
- Java马士兵高并发编程视频学习笔记(一)
			1.同一个资源,同步和非同步的方法可以同时调用 package com.dingyu; public class Y { public synchronized void m1() { System. ... 
- 7;XHTML表单
			1.表单的功能结构 2.文本栏.密码栏.隐藏栏 3.复选栏.单选栏 4.窗体栏位.区块栏位 5.按钮.图像按钮 6.允许上传文件 7.表单的外框和标题 8.元件的次序和快捷键 表单是提供让读者在网页上 ... 
- wamp安装运行时出现服务未启动
			安装wamp时,弹出对话框:Aestan Tray Menu Could not execute menu item (internal error )[Exception]could not ser ... 
- [转]原生JS-查找相邻的元素-siblings方法的实现
			在针对element的操作里,查找附近的元素是一个不可少的过程,比如在实现tab时,其中的一个div增加了“on”class,其他的去除“on”class.如果用jquery的朋友就肯定不会陌生sib ... 
- 一位ML工程师构建深度神经网络的实用技巧
			一位ML工程师构建深度神经网络的实用技巧 https://mp.weixin.qq.com/s/2gKYtona0Z6szsjaj8c9Vg 作者| Matt H/Daniel R 译者| 婉清 编辑 ... 
- 吴恩达机器学习笔记 —— 19 应用举例:照片OCR(光学字符识别)
			http://www.cnblogs.com/xing901022/p/9374258.html 本章讲述的是一个复杂的机器学习系统,通过它可以看到机器学习的系统是如何组装起来的:另外也说明了一个复杂 ... 
- 网络基础 记一次HTTPS证书验证测试过程
			记一次HTTPS证书验证测试过程 by:授客 QQ:1033553122 实践 1) 安装证书 选择主机A(假设10.202.95.88)上安装https证书 说明:采用https的服务器,必须安装数 ... 
