标签:names mes return tac turn bsp cout vector star
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#define long long LL
using namespace std;
vector<int>a[100005];
int pre[100005];
void dfs(int start,int m)
{
for(int i=0;i<a[start].size();i++)
{
if(pre[a[start][i]]!=0)
continue;
pre[a[start][i]]=start;
dfs(a[start][i],m);
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
int m,start;
memset(a,0,sizeof(a));
memset(pre,0,sizeof(pre));
cin>>m>>start;
pre[start]=-1;//它本身前缀为-1
int k1,k2;
for(int i=0;i<m-1;i++)
{
cin>>k1>>k2;
a[k1].push_back(k2);
a[k2].push_back(k1);
}
dfs(start,m);
for(int i=1;i<=m-1;i++)
cout<<pre[i]<<" ";
cout<<pre[m]<<endl;
}
return 0;
}
标签:names mes return tac turn bsp cout vector star
原文地址:https://www.cnblogs.com/koris-yyf/p/9029985.html