Firetruck(UVA 208)
测评传送门
思路:
这是道独流搜索,题目出的很好,输出给毁了。一定注意!!不要多打空格!!!
由于图会过于稠密,所以我们需要通过并查集剪枝,看从起点能不能到终点
code
#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int MXN=25; vector<int> v[MXN]; int n,fire,ub,ans; int fa[MXN],rout[MXN]; bool vis[MXN]; void ycl() { for(int i=1;i<=MXN;++i) { v[i].clear(); fa[i]=i; } } int Find(int x) { return x==fa[x]?x:fa[x]=Find(fa[x]); } void dfs(int x,int pos) { rout[pos]=x; if(x==fire) { ans++; printf("%d",rout[1]); for(int i=2;i<=pos;++i) { printf(" %d",rout[i]); } printf("\n"); return ; } for(int i=0;i<v[x].size();++i) { int to=v[x][i]; if(!vis[to]) { vis[to]=1; dfs(to,pos+1); vis[to]=0; } } } int main() { int cas=0; while(scanf("%d",&fire)!=EOF) { ycl(); int from,to; while(scanf("%d%d",&from,&to) && from && to) { v[from].push_back(to); v[to].push_back(from); fa[Find(from)]=Find(to); ub=max(ub,max(from,to)); } ans=0; printf("CASE %d:\n",++cas); if(Find(1)!=Find(fire)) { printf("There are %d routes from the firestation to streetcorner %d.\n",0,fire); } else { for(int i=1;i<=ub;++i) { if(v[i].size()) { sort(v[i].begin(),v[i].end()); } } memset(vis,0,sizeof(vis)); vis[1]=1; dfs(1,1); printf("There are %d routes from the firestation to streetcorner %d.\n",ans,fire); } } } /* 6 1 2 1 3 3 4 3 5 4 6 5 6 2 3 2 4 0 0 4 2 3 3 4 5 1 1 6 7 8 8 9 2 5 5 7 3 1 1 8 4 6 6 9 0 0 */
从0到1很难,但从1到100很容易