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 ...
随机推荐
- Tiny210编译和烧写u-boot步骤
当有多个交叉编译器是,不方便设置环境变量时,可以在编译命令中指定交叉编译器,具体如下: make ARCH=arm CROSS_COMPILE=/opt/FriendlyARM/toolschain/ ...
- python 第三方库下载
C:\Python27\Scripts 路径下: easy_install.exe: C:\Python27\Scripts>easy_install.exe pycrypto pip.exe: ...
- Node.js学习 - Function
Node.js函数和JavaScript类似 function say(word) { console.log(word); } function execute(someFunction, valu ...
- OpenCv的Java,C++开发环境配置
1.OpenCV 下载及安装配置 opencv的下载地址:http://opencv.org/downloads.html 最新版本:opencv3.0.0 注意:支持的visual studio20 ...
- 错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 的处理方法
- hdu_3886_Final Kichiku “Lanlanshu”(数位DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3886 题意:这题的题意有点晦涩难懂,大概意思就是给你一个区间,让你找一些满足递增递减条件的数,举个列: ...
- Django之路:QuerySet API,后台和表单
一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...
- (转)hadoop三个配置文件的参数含义说明
hadoop三个配置文件的参数含义说明 1 获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配 ...
- mr本地运行的几种模式
MR程序的几种提交运行模式 本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路 ...
- 序列化为XML
java类序列化成xml 方法[转] 今天看了下JAVA序列化.还是一知半解.怎么也没有弄明白,怎么序列化成XML文件.处入半解状态.在网上找了很多,大部分是理论上的.没有实际的例子.功夫不负有心人, ...