题目大意

给定一棵树,要求你按给定的叶子节点顺序对整棵树进行遍历,并且恰好经过2*n-1个点,输出任意一条符合要求的路径

题解

每次从叶子节点开始遍历到上一个叶子节点就OK了, 这个就是符合要求的路径

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MAXN 305
vector<int>G[MAXN],ans;
bool dfs(int rt,int u,int fa)
{
if(rt==u) return true;
int len=G[u].size();
for(int i=0;i<len;i++)
{
int v=G[u][i];
if(fa==v) continue;
if(dfs(rt,v,u))
{
ans.push_back(u);
return true;
}
}
return false;
}
int main()
{
//freopen("tree.txt","r",stdin);
int n;
scanf("%d",&n);
for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
ans.push_back(1);
int rt=1,v;
while(scanf("%d",&v)!=EOF)
{
dfs(rt,v,-1);
rt=v;
}
dfs(rt,1,-1);
if(ans.size()!=(2*n-1)) printf("-1\n");
else
{
for(size_t i=0;i<ans.size();i++) printf("%d ",ans[i]);
printf("\n");
}
return 0;
}

CF29D - Ant on the Tree(DFS)的更多相关文章

  1. codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)

    D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. Codeforces 29D Ant on the Tree 树的遍历 dfs序

    题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径.要求遍历叶子节点时依照给定叶子节点的先后顺序訪问. 思路: 给每 ...

  4. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  5. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  6. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  7. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  8. Codeforces Round #321 (Div. 2)C(tree dfs)

    题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...

  9. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...

随机推荐

  1. Vijos P1061 迎春舞会之三人组舞 DP

    题目链接:https://vijos.org/p/1061 n个人选出3*m人,排成m组,每组3人. 站的队形——较矮的2个人站两侧,最高的站中间. 从对称学角度来欣赏,左右两个人的身高越接近,则这一 ...

  2. HDU 1969 Pie(二分法)

    My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N ...

  3. Java 内存区域

    Java虚拟机锁管理的内存包括以下几个运行时数据区域: 1.程序计数器: 程序计数器是一块较小的内存空间,它的作用可以看做是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里,字节码解释器工作时 ...

  4. Oracle登陆及修改用户密码

    16:20 2013/7/7 Oracle 登陆 管理员登陆 sqlplus sys/root as sysdba (密码认证)这个root是安装数据库最初你输入的口令 sqlplus     /as ...

  5. 我的PHP之旅--SQL语句

    SQL语句 结构化查询语言(Structured Query Language)简称SQL,是一种操作数据的语言. 增加记录 INSERT INTO table_name(字段1, 字段2, 字段3) ...

  6. jquery 插件页面回到顶部

    引用: jquery.scrollUp.min.js js: $.scrollUp({ scrollName: 'scrollUp', // Element ID topDistance: '300' ...

  7. .bat批处理脚本让cmd命令行提示符cd到工作目录 (转)

    打开cmd,检查命令行提示符所在的默认位置(目录),进入该目录用notepad++创建一个文件,输入 @echo offrem 这个符号表示该行是注释.rem 进入f盘,需要先切换盘符,成功后才能进入 ...

  8. Computer Vision的尴尬---by林达华

    Computer Vision的尴尬---by林达华 Computer Vision是AI的一个非常活跃的领域,每年大会小会不断,发表的文章数以千计(单是CVPR每年就录取300多,各种二流会议每年的 ...

  9. 转--Server “**” has shut down the connection prematurely一例分析

    近几天在性能测试过程中,发现loadrunner Controller经常报 Server “**” has shut down the connection prematurely .概率很高,现象 ...

  10. 李洪强漫谈iOS开发[C语言-035]-选择结构-与小结