要输出所有路径,又要字典序,dfs最适合了,用并查集判断1和目的地是否连通即可

#include<bits/stdc++.h>
using namespace std;
const int maxn = ; int p[maxn],cnt[maxn];
void init(int n) {
for(int i = ;i <= n; i++) p[i] = i,cnt[i] = ;
}
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("");
for(int i = ; i < d; i++)
printf(" %d",path[i]);
puts("");
Count++;
return;
}
if(d == maxd) return;
for(int i = ; i < deg[cur]; i++){
int v = G[cur][i];
if(vis[v]) continue;
vis[v] = ;
path[d] = v;
dfs(d+,v);
vis[v] = ;
}
} int main()
{
//freopen("in.txt","r",stdin);
int cas = ;
while(~scanf("%d",&k)){
printf("CASE %d:\n",++cas);
int u,v,n = ;
init();
memset(G0,,sizeof(G0));
memset(deg,,sizeof(deg));
while(scanf("%d%d",&u,&v),u){
G0[u][v] = G0[v][u] = ;
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()) {printf("There are 0 routes from the firestation to streetcorner %d.\n",k); continue;}
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) if(G0[i][j]){
G[i][deg[i]++] = j;
}
maxd = cnt[Find()] ;
memset(vis,,sizeof(vis));
vis[] = ;
Count = ;
dfs(,);
printf("There are %d routes from the firestation to streetcorner %d.\n",Count,k);
}
return ;
}

最新文章

  1. 【JS基础】
  2. 北京54全国80及WGS84坐标系的相互转换
  3. java 线程安全 synchronized
  4. ndk学习10: linux文件系统
  5. 创建Struct2的web应用(一)
  6. 快速安装 GitLab 并汉化
  7. 【IOS笔记】Resource Management in View Controllers
  8. C语言开源项目
  9. html中调用silverlight中的方法
  10. C++ (P103—P154)
  11. 武汉科技大学ACM:1010: 零起点学算法89——母牛的故事
  12. iOS手机号正则表达式并实现344格式 (正则的另一种实现方式)
  13. poj 1715 Hexadecimal Numbers 排列组合
  14. jQuery从入门到忘记
  15. 如何用while循环输出十行十列变色★☆
  16. 细数本地连阿里云上mysql8遇到的坑
  17. no-sql数据库之redis
  18. python操作excel的读、计算、写----xlrd、copy
  19. Java JDBC基本用法
  20. 7z压缩与解压命令

热门文章

  1. eval解析字符串问题
  2. Centos7.2 下安装配置pip
  3. UVaLive 3530 Martian Mining (简单DP)
  4. Unity DOTS 走马观花
  5. 2014-10-6 NOIP模拟赛
  6. 洛谷P3768 简单的数学题(莫比乌斯反演+狄利克雷卷积+杜教筛)
  7. Centos下磁盘管理的常用命令记录(如查找大文件)
  8. HTML+CSS注意点
  9. jquery.jscrollpane.js滚动速度设置
  10. Luogu P3166 [CQOI2014]数三角形 组合数学