UVA - 208 Firetruck(消防车)(并查集+回溯)
题意:输入着火点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(消防车)(并查集+回溯)的更多相关文章
- UVA - 208 Firetruck(并查集+dfs)
题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...
- UVa 208 - Firetruck 回溯+剪枝 数据
题意:构造出一张图,给出一个点,字典序输出所有从1到该点的路径. 裸搜会超时的题目,其实题目的数据特地设计得让图稠密但起点和终点却不相连,所以直接搜索过去会超时. 只要判断下起点和终点能不能相连就行了 ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
- UVA 12232 Exclusive-OR(并查集+思想)
题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...
- UVA - 1197 (简单并查集计数)
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- UVA 10158 War(并查集)
//思路详见课本 P 214 页 思路:直接用并查集,set [ k ] 存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k 的敌人 所在集合的代表元素. #include< ...
- UVA - 11987 Almost Union-Find 并查集的删除
Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...
- uva 6910 - Cutting Tree 并查集的删边操作,逆序
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVA 11987 Almost Union-Find 并查集单点修改
Almost Union-Find I hope you know the beautiful Union-Find structur ...
随机推荐
- java代码用dom4j解析xml文件的简单操作
时间: 2016/02/17 目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中. 从网上搜了下,有四种常用的解析xml的 ...
- centos yum源问题
在配置CentOS-6.0-x86_64-bin-DVD2.iso作为本地yum源的时候,碰到相当多的问题: ----------------------------------------- 问题 ...
- 初识Selenium(三)
浅谈基于Selenium的Web自动化测试框架 发表于:2011-4-25 10:58 作者:邵育亮 来源:51Testing软件测试网原创 字体:大 中 小 | 上一篇 | 下一篇 | 打印 ...
- 简单时钟——css3
这里我们使用css3的特性制作一个简易的时钟,代码如下: <!DOCTYPE html><html> <head> <meta charset="U ...
- 转:loadrunner ---循环输出关联数组
web_reg_save_param,将Ord参数值设定为ALL,则关联函数将自动把符合条件的关联值保存到参数数组里.在本例中,假设关联值返回三条记录,则LR分别将值保存到sor_1,sor_2,so ...
- ping命令使用技巧(一次Ping多个地址)
打开windows 命令行 窗口, 在命令行输入以下命令: for /l %i in (1,1,255) do ping -n 1 -w 60 192.168.0.%i | find " ...
- [转载] ASP.NET MVC4使用百度UEDITOR编辑器
前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...
- AutoFac使用方法总结
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...
- English--Computer System
A: Hey, Bill, Can you tell what's wrong with my computer? I can't move the mouse, I can's user the k ...
- 判断非法字符串的类方法,与jsp
private String_do_judge judge; if (judge.isContain(key)) { return "feifa"; } 上面这写代码添加到进入ac ...