题意,N座城市有N-1条路,目的是找到哪个城市可以到目的城市
//NYOJ--search--吝啬的国度
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
vector<int> city[100005];
int road[100004];
int N,S;
void dfs(int);
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","a+",stdout);
int M;cin>>M;
while(M--){
memset(city,0,sizeof(city));
memset(road,0,sizeof(road));
cin>>N>>S;
int i;
road[S] = -1;
for(i=0; i<N-1; ++i){
int a,b;cin>>a>>b;
city[a].push_back(b);
city[b].push_back(a);
}
dfs(S);
for(i=1; i<=N; ++i){
cout<<road[i]<<" ";
}
cout<<endl;
}
return 0;
}
void dfs(int i){
for(vector<int>::iterator iter=city[i].begin(); iter!=city[i].end(); iter++){
if(road[(*iter)])continue;
road[(*iter)] = i;
dfs((*iter));
}
}