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. Java常用代码工具类相关

    1.HttpServletRequest转换成Map public static Map<String,String> parseXML(HttpServletRequest reques ...

  2. 九度OJ 1196:成绩排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4339 解决:1476 题目描述: 用一维数组存储学号和成绩,然后,按成绩排序输出. 输入: 输入第一行包括一个整数N(1<=N< ...

  3. Webpack探索【1】--- 基础知识

    本文主要说明Webpack的一些基础内容.

  4. node.js版本升级

      node有一个模块叫n(这名字可够短的...),是专门用来管理node.js的版本的. 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stabl ...

  5. activiti踩坑

    最近在学习activiti,偶然间遇到一个错误:加载引擎的时候报错,显示空指针错误,跟代码发现初始化配置文件返回为null.几经排查,可能是因为我发布流程后又清空了数据库数据导致的.然后我把表全部删除 ...

  6. 使用基本 SQL 命令

    概述 在本教程中,将学习结构化查询语言 (SQL),包括: 使用基本 SQL 命令 执行基本数据操做 数据库和 SQL 在本系列教程中,目前我们使用平面文本文件来存储数据.平面文本文件可能适合相对较少 ...

  7. 免费好用的Diff和Merge工具大总结

    总结:比较下来:diffmerge和P4merge最好用,kdiff比较专业些,支持自动merge. 一 csdiff 下载:http://www.componentsoftware.com/Prod ...

  8. c的详细学习(8)指针学习(二)

    (1)指针与二维数组 一个数组的名字代表该数组的的首地址,是地址常量(作为形式参数的数组名除外),这一规定对二维数组或更高维数组同样适用. 在c语言中定义的任何一个二维数组实际上都可以看做是一个一维数 ...

  9. spring-boot3

    更多的配置: # =================================================================== # COMMON SPRING BOOT PR ...

  10. runtime-分类为什么不生成setter和getter

    前言 前几天有人问我一个问题:为什么分类不能自动创建get set方法.老实说,笔者从来没有去思考过这个问题.于是这次通过代码实践跟runtime源码来探究这个问题. 准备工作 为了能减少输出类数据的 ...