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. 要胀爆的Angular1.0

    尝试从http请求上遏制缓存: http://blog.csdn.net/u010039979/article/details/54376856 if (!$httpProvider.defaults ...

  2. 25线程基础-CLR

    由CLR via C#(第三版) ,摘抄记录... 1.线程是CPU的虚拟化,windows为每个进程提供专用线程(CPU)2.线程开销:内存和时间. 线程内核对象—OS为系统中创建的每个线程都分配并 ...

  3. 【BZOJ4205】卡牌配对 最大流

    [BZOJ4205]卡牌配对 Description 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值 ...

  4. 阿里云+LAMP环境配置

    1. 准备好一键Linux环境的脚本: http://dwz.cn/6Nlexm 2. 运行命令:# yum install lynx tree nmap sysstat lrzsz dos2unix ...

  5. Python菜鸟之路:sqlalchemy/paramiko进阶

    前言:ORM中的两种创建方式 数据库优先:指的是先创建数据库,包括表和字段的建立,然后根据数据库生成ORM的代码,它是先创建数据库,再创建相关程序代码 代码优先:就是先写代码,然后根据代码去生成数据库 ...

  6. ElasticSearch(十八)初识分词器

    1.什么是分词器 作用:切分词语,normalization(提升recall召回率),如给你一段句子,然后将这段句子拆分成一个一个的单个的单词,同时对每个单词进行normalization(时态转换 ...

  7. ElasticSearch(十七)初识倒排索引

    现在有两条document: doc1:I really liked my small dogs, and I think my mom also liked them. doc2:He never ...

  8. BZOJ1791: [Ioi2008]Island 岛屿

    BZOJ1791: [Ioi2008]Island 岛屿 Description 你将要游览一个有N个岛屿的公园. 从每一个岛i出发,只建造一座桥. 桥的长度以Li表示. 公园内总共有N座桥. 尽管每 ...

  9. Java语言实现简单FTP软件------>远程文件管理模块的实现(十)

    首先看一下界面: 1.远程FTP服务器端的文件列表的显示 将远程的当前目录下所有文件显示出来,并显示文件的属性包括文件名.大小.日期.通过javax.swing.JTable()来显示具体的数据.更改 ...

  10. HDOJ 3473 Minimum Sum

    划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K ...