ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

CF29D Ê÷µÄ±éÀú
2015-07-20 17:23:09 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:1´Î
Tags£ºCF29D

?

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.

Sample test(s) input
3
1 2
2 3
3
output
1 2 3 2 1 
input
6
1 2
1 3
2 4
4 5
4 6
5 6 3
output
1 2 4 5 4 6 4 2 1 3 1 
input
6
1 2
1 3
2 4
4 5
4 6
5 3 6
output
-1
/**
CF29D Ê÷µÄ±éÀú
ÌâÄ¿´óÒ⣺
         ¸ø¶¨Ò»¸öÊ÷ÒÔ1Ϊ¸ù½Úµã£¬¸ø¶¨Ã¿¸öÒ¶×Ó½ÚµãµÄ±éÀú˳Ðò£¬´Ó1³ö·¢°´ÕÕ´Ë˳Ðò±éÀú×îºóÔٻص½1£¬
         ¾­¹ýûÌõ±ß2´Î£¬ÎÊÊÇ·ñ¿ÉÒÔ£¬Èô¿ÉÒÔÊä³öÖ®
½âÌâ˼·£º
        ÕâÊÇÒ»¿ÃÊ÷£¨ÎÞÏòÎÞ»·Á¬Í¨Í¼£©£¬ÄÇôÎÒÃDZéÀúµÄʱºò¼ÙÉèÒ»Ö±µÄÒ¶×Ó½Úµã·Ö±ðΪa1,a2,a3£¬
        ÄÇôÎÒÃÇÏÈdfs³ö1~a1Ö®¼äµÄ·£¬½Ó×Åa1~a2£¬a2~a3,a3~1,¹ý³ÌÖÐͳһ·ÅÔÚÒ»¸öÈÝÆ÷ÖУ¬×îºóÈôÕýºÃ
        ÊÇ2*n-1¸öµã¼ÓÈëÈÝÆ÷£¬ÄÇô¾ÍÓн⣨ÎÞÏòÎÞ»·Í¼£¬µãuµ½µãvµÄ·¾¶Èç¹ûÓÐÄǾÍÖ»ÓÐÒ»Ìõ£¬ËµÃ÷Õâ¸ö
        ·½·¨¿ÉÐУ©£¬Êä³ö¾ÍºÃÁË
*/
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        using namespace std; int n; vector
       
         vec[500],a; bool dfs(int root,int u,int pre) { if(root==u)return 1; for(int i=0; i
        
         

?

¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºuva 140 Bandwidth £¨È«ÅÅÁÐ+±©Á¦.. ÏÂһƪ£ºPOJ 2923 Relocation(״ѹ+±³°ü£©

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Spring Boot Java£º (2025-12-26 16:20:19)
¡¤Spring Boot¤ÇHello (2025-12-26 16:20:15)
¡¤Spring ¤Î»ù±¾¤«¤éŒ (2025-12-26 16:20:12)
¡¤C++Ä£°å (template) (2025-12-26 15:49:49)
¡¤C ÓïÑÔÖÐÄ£°åµÄ¼¸ÖÖ (2025-12-26 15:49:47)