Codeforces Beta Round #29 (Div. 2, Codeforces format)--D. Ant on the Tree--DFS

D. Ant on the Tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Connected undirected graph without cycles is called a tree. Trees is a class of graphs which is interesting not only for people, but for ants too.

An ant stands at the root of some tree. He sees that there are n vertexes in the tree, and they are connected by n - 1 edges so that there is a path between any pair of vertexes. A leaf is a distinct from root vertex, which is connected with exactly one other vertex.

The ant wants to visit every vertex in the tree and return to the root, passing every edge twice. In addition, he wants to visit the leaves in a specific order. You are to find some possible route of the ant.

Input

The first line contains integer n (3 ≤ n ≤ 300) — amount of vertexes in the tree. Next n - 1 lines describe edges. Each edge is described with two integers — indexes of vertexes which it connects. Each edge can be passed in any direction. Vertexes are numbered starting from 1. The root of the tree has number 1. The last line contains k integers, where k is amount of leaves in the tree. These numbers describe the order in which the leaves should be visited. It is guaranteed that each leaf appears in this order exactly once.

Output

If the required route doesn't exist, output -1. Otherwise, output 2n - 1 numbers, describing the route. Every time the ant comes to a vertex, output it's index.

Examples

input

Copy

3
1 2
2 3
3

output

Copy

1 2 3 2 1 

input

Copy

6
1 2
1 3
2 4
4 5
4 6
5 6 3

output

Copy

1 2 4 5 4 6 4 2 1 3 1 

input

Copy

6
1 2
1 3
2 4
4 5
4 6
5 3 6

output

Copy

-1

给出点与点之间的关系,并且给出叶子节点,问是否能不能按给定叶子节点访问整颗树且每条边都访问两次。

 

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <string>     //字符串类
#include <vector>     //STL动态数组容器
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 600+66;
const int maxm=90000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
map<int,int>mmp;
struct Edge
{
    int u, v;
    int next;
    Edge() {}
    Edge(int u, int v, int next): u(u), v(v), next(next) {}
};

struct Graph
{
    Edge edge[maxm];
    int head[maxn], etop;

    void init(int nn)
    {
        etop = 0;
        rep(i,0,nn+6)
        {
            head[i]=-1;//
        }
    }

    void Add_Edge(int u, int v)
    {
        edge[++etop] = Edge(u, v, head[u]);
        head[u] = etop;
        edge[++etop] = Edge(v, u, head[v]);
        head[v] = etop;
    }
} G;
bool vis[maxn];
int n;
int du[maxn];
vector<int>ans;
vector<int>g[maxn];
bool dfs(int root,int u,int pre)
{
    if(u==root)
    {
        return 1;
    }
    rep(i,0,g[u].size()-1)
    {
        int v=g[u][i];
        if(v!=pre)
        {
            if(dfs(root,v,u))
            {
                ans.push_back(u);
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n))
    {
        G.init(n);
        rep(i,1,n-1)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
            du[u]++;
            du[v]++;
        }
        ans.push_back(1);
        int root=1;
        rep(i,2,n)
        {
            if(g[i].size()==1)
            {
                int u;
                scanf("%d",&u);
                dfs(root,u,-1);
                root=u;
            }
        }
        dfs(root,1,-1);
        if(ans.size()!=2*n-1)
        {
            printf("-1\n");
        }
        else
        {
            rep(i,0,2*n-3)
            {
                printf("%d ",ans[i]);
            }
            printf("%d\n",ans[2*n-2]);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值