codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)
2 seconds
256 megabytes
standard input
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.
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.
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.
3
1 2
2 3
3
1 2 3 2 1
6
1 2
1 3
2 4
4 5
4 6
5 6 3
1 2 4 5 4 6 4 2 1 3 1
6
1 2
1 3
2 4
4 5
4 6
5 3 6
-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,最近公共祖先)的更多相关文章
- CodeForces 29D Ant on the Tree
洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- [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 ...
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径.要求遍历叶子节点时依照给定叶子节点的先后顺序訪问. 思路: 给每 ...
- Jamie and Tree (dfs序 + 最近公共祖先LCA)
题面 题解 我们求它子树的权值和,一般用dfs序把树拍到线段树上做. 当它换根时,我们就直接把root赋值就行了,树的结构不去动它. 对于第二个操作,我们得到的链和根的相对位置有三种情况: 设两点为A ...
- LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...
- [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 ...
- 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 ...
随机推荐
- 2016 acm香港网络赛 F题. Crazy Driver(水题)
原题网址:https://open.kattis.com/problems/driver Crazy Driver In the Linear City, there are N gates arra ...
- json:js和jquery中轻量级数据交换格式
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...
- Miller-Rabin大素数测试模板
根据费马小定理: 对于素数n,a(0<a<n),a^(n-1)=1(mod n) 如果对于一个<n的正整数a,a^(n-1)!=1(mod n),则n必不是素数. 然后就可以随机生成 ...
- Spring 和 filter
标题是 spring和filter,但是这里却是说的spring MVC 项目中需要用到filter,filter中需要用到spring实例化的bean,于是为了简化就形成spring和filter了 ...
- 洛谷P1038 神经网络==codevs1088 神经网络
P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...
- [转]React表单无法输入原因----约束性和非约束性组件
转自:http://blog.csdn.net/lihongxun945/article/details/46730835 表单是前端非常重要的一块内容,并且往往包含了错误校验等逻辑. React对表 ...
- iostat命令简单使用
1.iostat使用范围 iostat命令可以生成3种类型的报告: (1)CPU使用情况的报告 (2)设备使用情况的报告 (3)网络文件系统(NFS)使用情况的报告 2.每种报告的格式说明 关于CPU ...
- Linux安装python3.7.2详细操作步骤
1.下载Python依赖环境 yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ...
- 我的Android进阶之旅------>Android二级ListView列表的实现
实现如下图所示的二级列表效果 首先是在布局文件中,布局两个ListView,代码如下: <LinearLayout xmlns:android="http://schemas.andr ...
- spring AOP理解和相关术语
一.AOP理解 AOP:横向抽取机制,底层使用代理方式实现. 示例: 现有LogDAO接口以及实现Log接口的Log类.类有add的方法,现在要打印add方法的开始时间和结束时间.(即增强Log的ad ...