UVA208 Firetruck 消防车(并查集,dfs)
2015-07-10 18:51
381 查看
要输出所有路径,又要字典序,dfs最适合了,用并查集判断1和目的地是否连通即可
#include<bits/stdc++.h> using namespace std; const int maxn = 21; int p[maxn],cnt[maxn]; void init(int n) { for(int i = 1;i <= n; i++) p[i] = i,cnt[i] = 1; } int Find(int x) { return p[x] == x ? x : p[x] = Find(p[x]); } bool G0[maxn][maxn]; int G[maxn][maxn],deg[maxn]; int maxd; int path[maxn]; bool vis[maxn]; int k,Count; void dfs(int d,int cur) { if(cur == k){ printf("1"); for(int i = 1; i < d; i++) printf(" %d",path[i]); puts(""); Count++; return; } if(d == maxd) return; for(int i = 0; i < deg[cur]; i++){ int v = G[cur][i]; if(vis[v]) continue; vis[v] = 1; path[d] = v; dfs(d+1,v); vis[v] = 0; } } int main() { //freopen("in.txt","r",stdin); int cas = 0; while(~scanf("%d",&k)){ printf("CASE %d:\n",++cas); int u,v,n = 0; init(20); memset(G0,0,sizeof(G0)); memset(deg,0,sizeof(deg)); while(scanf("%d%d",&u,&v),u){ G0[u][v] = G0[v][u] = 1; n = max(n,v); n = max(n,u); int s1 = Find(u), s2 = Find(v); if(s1 != s2) {p[s1] = s2; cnt[s2] += cnt[s1];} } if(Find(k) != Find(1)) {printf("There are 0 routes from the firestation to streetcorner %d.\n",k); continue;} for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(G0[i][j]){ G[i][deg[i]++] = j; } maxd = cnt[Find(1)] ; memset(vis,0,sizeof(vis)); vis[1] = 1; Count = 0; dfs(1,1); printf("There are %d routes from the firestation to streetcorner %d.\n",Count,k); } return 0; }
相关文章推荐
- aircrack-ng 套件破解wifi(WPA)
- Spring+Mybatis错误1
- BZOJ 3589 动态树 树链拆分+纳入和排除定理
- orcale sql常用语句
- 常见路由器默认登录用户名和密码(大全)
- NYOJ 461-Fibonacci数列(四)(求斐波那契数列前4位)
- Java设计模式之观察者模式
- ACM-数据结构总览
- 常见路由器默认登录用户名和密码(大全)
- iOS学习笔记-LocalAuthentication
- php的数据类型
- SQL Server 导入 MDF LDF文件
- PCIe SSD 性能调优--概述篇
- JSP——JSTL标签库
- winfrom——计算机进制转换
- Java JDBC批处理优化
- ViewPager使用方法
- Json入门
- 作为一个新人,怎样学习嵌入式Linux,(韦东山)
- 阿里知名工程师