题目描述:
http://acm.nyist.net/JudgeOnline/problem.php?pid=20
1
10 1
1 9
1 8
8 10
10 3
8 6
1 2
10 4
9 5
3 7
-1 1 10 10 9 8 3 1 1 8
题目分析:
不要一位此题只是简单的单联通问题,此题是个深度搜索问题,正常的搜索问题,可能是题目没有描述清楚,本题代码参考的大神的代码。。。
AC代码:
/**
*dfs,深度搜索,
*注意找到最好的路线,从A能到b,那么从b必然也能到a
*/
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<cmath>
using namespace std;
int map_vis[100005];
void a_prior_dfs(int a){
int p=map_vis[a];
if(p!=0){//继续向前搜素
a_prior_dfs(p);
map_vis[p]=a;//反向标记
}
}
int main()
{
int t,n,s;
scanf("%d",&t);
while(t--){
memset(map_vis,0,sizeof(map_vis));
scanf("%d%d",&n,&s);
int a,b;
for(int i=0;i<n-1;i++){
scanf("%d%d",&a,&b);
if(map_vis[b]==0) map_vis[b]=a;
else{
a_prior_dfs(a);
map_vis[a]=b;//反向标记
}
}
a_prior_dfs(s);//从出发点继续搜索
map_vis[s]=-1;
for(int i=1;i<=n;i++){
printf("%d ",map_vis[i]);
}
printf("\n");
}
return 0;
}
原文:http://blog.csdn.net/fool_ran/article/details/42065593