#include<bits/stdc++.h>
using namespace std;
#define maxn 500
struct Edge{
int to,nxt;}edge[maxn<<
1];
int n,head[maxn],tot,a[maxn],k;
vector<
int>
v;
void init(){
memset(head,-
1,
sizeof head);
k=tot=
0;
}
void addedge(
int u,
int v){
edge[tot].to=v;edge[tot].nxt=head[u];head[u]=tot++
;
}
bool dfs(
int root,
int u,
int pre)
{
if(root==u)
return 1;
for(
int i=head[u]; i>=
0; i=
edge[i].nxt)
{
int vv=
edge[i].to;
if(vv==pre)
continue;
if(dfs(root,vv,u))
{
v.push_back(u);
return 1;
}
}
return 0;
}
int main(){
init();
cin>>
n;
for(
int i=
1;i<n;i++
){
int u,v;cin>>u>>
v;
addedge(u,v);
addedge(v,u);
}
int u;
while(cin>>u)a[++k]=
u;
v.push_back(1);
dfs(1,a[
1],
0);
int last=a[
1];
for(
int i=
2;i<=k;i++
)
dfs(last,a[i],0),last=
a[i];
dfs(a[k],1,
0);
if(v.size()!=
2*n-
1)puts(
"-1");
else {
for(
int i=
0;i<v.size();i++
)
printf("%d ",v[i]);
printf("\n");
}
}
转载于:https://www.cnblogs.com/zsben991126/p/10333330.html