UVA 208 并查集+dfs
来源:互联网 发布:linux send 返回值 编辑:程序博客网 时间:2024/05/18 02:46
//http://acm.hust.edu.cn/vjudge/problem/19858//要先判断N与1是否联通,以及要搜索的点应该与N联通。#include <stdio.h>#include <string>#include <cstring>#include <queue>#include <algorithm>#include <functional>#include <vector>#include <sstream>#include <iomanip>#include <math.h>#include <iostream>#include <sstream>#include <time.h>#include <stack>#include <set>#include <map>#include <time.h>#include <bitset>using namespace std;const int MAX=25;int N,a,b,M;int G[MAX][MAX];int pre[MAX];bool used[MAX];int Ans[MAX],Cnt;int Find(int x){ int r=x; while(r!=pre[r]) r=pre[r]; int i=x,j; while(pre[i]!=r) { j=pre[i]; pre[i]=r; i=j; } return r;}void mix(int x,int y){ int fx=Find(x),fy=Find(y); if(fx!=fy) pre[fy]=fx;}void dfs(int x,int l){ Ans[l]=x; if (x==N) { for (int i=0;i<=l;i++) cout<<Ans[i]<<(i==l?'\n':' '); Cnt+=1; return; } for (int i=1;i<MAX;i++) { if (!used[i]&&i!=x&&Find(i)==M&&G[x][i]==1) { used[i]=true; dfs(i,l+1); used[i]=false; } }}int main(){ cin.sync_with_stdio(false); int cases=1; while (cin>>N) { for (int i=0;i<MAX;i++) pre[i]=i; Cnt=0; memset(G,0,sizeof(G)); memset(used,false,sizeof(used)); memset(Ans,0,sizeof(Ans)); cout<<"CASE "<<cases++<<":\n"; while (cin>>a>>b) { if (a==0&&b==0) break; G[a][b]=G[b][a]=1; mix(a,b); } if (Find(1)==Find(N)) { M=Find(1); used[1]=true; dfs(1,0); } cout<<"There are "<<Cnt<<" routes from the firestation to streetcorner "<<N<<".\n"; } return 0;}
0 0
- UVA 208 并查集+dfs
- UVA 208 Firetruck DFS 并查集
- uva 208 Firetruck (DFS + 并查集)
- UVA 208 - Firetruck DFS 并查集剪枝
- Uva 208 Firetruck(dfs并查集优化)
- UVA 10608 dfs或者并查集
- 并查集+DFS
- POJ1291-并查集/dfs
- 并查集思想+dfs
- hdu1198(DFS/并查集)
- uva 793 并查集
- uva 11503 并查集
- uva 11987 并查集
- Friends+uva+并查集
- UVA 10608并查集
- uva 10608(并查集)
- uva 10158(并查集)
- uva 1493(并查集)
- Gradle 动态编译-利用配置文件指定不同软件版本对应的服务器地址
- BZOJ 3680 模拟退火
- css表格练习
- socket 设置非阻塞
- 设计模式之原型模式
- UVA 208 并查集+dfs
- 白话scala系列五 匿名函数分析
- CocoaPods的安装使用及遇到的问题
- UVA 225 剪枝+dfs
- 郑大第九届校赛-正式赛-zzulioj-1812-sort
- Ugly Number
- Struts2简介以及初步搭建配置
- onethink源码success 和error 无法跳转链接
- 使用LeakCanary检测内存泄露