题目大意

给定一棵树,要求你按给定的叶子节点顺序对整棵树进行遍历,并且恰好经过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. STL容器的本质

    http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...

  2. Codeforces Round #327 div2

    Problem_A(591A): 题意: 有一段长度为l的路,两个人分别在两个端点,1, l. 现在已知每个人的速度为p,q. 求第一个人(初始位置在1)在他们第二次相遇的时候的位置. 当他们相遇的时 ...

  3. 标准初始化css样式表

    body { font-size:12px; line-height:1.3; font-family:'微软雅黑', Tahoma,Helvetica,Arial,'宋体', sans-serif; ...

  4. 移动js

    http://blog.sina.com.cn/s/blog_6553196001015672.html http://blog.sina.com.cn/s/blog_6553196001014vjb ...

  5. TSS 任务状态段

    TSS(任务状态段) 1 什么是TSS TSS 全称task state segment,是指在操作系统进程管理的过程中,任务(进程)切换时的任务现场信息.  2 TSS工作细节 TSS在任务切换过程 ...

  6. SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用<s:message>和ReloadableResourceBundleMessageSource实现国际化

    一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1 ...

  7. xcode5编译过的xib 如何在xcode4.6下打开

    IOS7一出来,对应的xcode版本变成了5了,这次xcode升级比较大,特别是在源代码编译方面,苹果下足了功夫,编译时间不到原来的一半,忽然强烈觉得android在这方面需要加强啊: 其他不多说,X ...

  8. Android 内核初识(8)Binder

    简介 Binder是Android系统提供的一种IPC(进程间通信)机制.由于Android是基于Linux内核的,因此,除了Binder外,还存在其他的IPC机制,例如管道和socket等.Bind ...

  9. net中System.Security.Cryptography 命名空间 下的加密算法

    .net中System.Security.Cryptography命名空间 在.NETFramework出现之前,如果我们需要进行加密的话,我们只有各种较底层的技术可以选择,如 Microsoft C ...

  10. SCOI2007排列perm

    1072: [SCOI2007]排列perm Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 805  Solved: 497[Submit][Stat ...