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

题意:给你一个无向图,先变成以1为根的树,在判断是否能用给的顺序访问叶子节点,每条边只走两次;

思路:先dfs变成tree,在找给的顺序相邻两叶子的最近公共祖先,把路径存下来,最后判断存下的路径里的节点是不是2*n-1个,是就输出路径;

AC代码:

#include <bits/stdc++.h>
using namespace std;
vector<int>v[302];
vector<int>ans;
stack<int>sta;
queue<int>qu;
int c[300],flag[303],fa[303];
int lca(int x,int y)
{
int start=x;
memset(flag,0,sizeof(flag));
flag[x]=1;
while(x!=1)
{
x=fa[x];
flag[x]=1;
}
while(!flag[y])
{
sta.push(y);
y=fa[y];
}
while(start!=y)
{
start=fa[start];
ans.push_back(start);
}
while(!sta.empty())
{
ans.push_back(sta.top());
sta.pop();
}
}
int bfs()
{
memset(flag,0,sizeof(flag));
while(!qu.empty()){
int fr=qu.front();
qu.pop();
int len=v[fr].size();
for(int i=0;i<len;i++)
{
if(flag[v[fr][i]]==0)
{
fa[v[fr][i]]=fr;
qu.push(v[fr][i]);
flag[v[fr][i]]=1;
}
}
}
}
int main()
{
int n,a,b;
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
qu.push(1);
bfs();
int k=0;
for(int i=2;i<=n;i++)
{
if(v[i].size()==1)k++;
}
for(int i=1;i<=k;i++)
{
scanf("%d",&c[i]);
}
ans.push_back(1);
c[k+1]=1;
c[0]=1;
for(int i=0;i<=k;i++)
{
lca(c[i],c[i+1]);
}
int len=ans.size();
if(len!=2*n-1)
{
cout<<"-1"<<endl;
return0;
  }
  for(int i=0;i<len;i++)printf("%d ",ans[i]);
return0;
}

求最近公共祖先用的最原始的方法,其实可以优化;还有就是最近一直喜欢用这些容器啥的,还是太笨啊!

codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)的更多相关文章

  1. CodeForces 29D Ant on the Tree

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...

  2. 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...

  3. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

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

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

  5. Jamie and Tree (dfs序 + 最近公共祖先LCA)

    题面 题解 我们求它子树的权值和,一般用dfs序把树拍到线段树上做. 当它换根时,我们就直接把root赋值就行了,树的结构不去动它. 对于第二个操作,我们得到的链和根的相对位置有三种情况: 设两点为A ...

  6. LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

    题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...

  7. [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先

    如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...

  8. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. 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 ...

随机推荐

  1. JavaScript提高:002:ASP.NET使用easy UI实现tab效果

    近期在做ASP.NET项目中,须要实现一个tab页控件. 发现asp.net控件中没找到现成的. 一般的实现都须要js和div配合.于是就用到了easyui里面的. 使用也非常easy.将easyui ...

  2. Python爬上不得姐 并将段子写入数据库

    #Python2.7 可以优化一下 前10页 每页点赞最多的段子 百思不得姐 # -*- coding: utf-8 -*-import MySQLdbimport urllib,urllib2imp ...

  3. 第三方-Swift2.0后Alamofire的使用方法

    第一部分,配置项目 首先我们创建一个工程如下图 在此只讲纯手打拉第三方框架的方法 然后把下载的Alamofire解压文件全部放进创建的项目文件夹中,如下图 关键时刻到了哦,集中精神,注意!!! 这个图 ...

  4. python tensorflow 安装

    我是先下载tensorflow-1.5.0rc1-cp36-cp36m-win32.whl,再执行命令行安装的 下载地址:https://pypi.python.org/pypi/tensorflow ...

  5. Linux 随手记(文件操作)

    新建文件夹 mkdir 文件夹名 新建文件 touch 文件名 重命名 mv 文件名 新文件名 将/a目录移动到/b下,并重命名为c mv /a /b/c 复制文件 cp [选项] 源文件或目录 目标 ...

  6. Android JNI开发之NDK环境搭建

    参考:http://www.cnblogs.com/yejiurui/p/3476565.html 谷歌改良了ndk的开发流程,对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本, ...

  7. c语言的编译和运行流程

    C语言源程序经过编译器进行词法分析 语法分析 等过程生成中间语言(object后缀的文件)编译期间会生成一个字符表和静态分配空间(如new static 全局变量)它们所需的内存空间可以计算出来放在链 ...

  8. [转]jquery中innerWidth(),outerWidth(),outerWidth(true)和width()的区别

    转自:http://www.cnblogs.com/keyi/p/5933981.html   jquery中innerWidth(),outerWidth(),outerWidth(true)和wi ...

  9. 开始翻译《Beginning SharePoint 2013 Development》

    伙同涂曙光@kaneboy 和柴晓伟@WindieChai 翻译Beginning SharePoint 2013 Development 作者是Steve Fox,传说中的Andrew Connel ...

  10. Java之线程池(二)

    关于线程和线程池的学习,我们可以从以下几个方面入手: 第一,什么是线程,线程和进程的区别是什么 第二,线程中的基本概念,线程的生命周期 第三,单线程和多线程 第四,线程池的原理解析 第五,常见的几种线 ...