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 ...
随机推荐
- 动态创建Lambda表达式实现高级查询
需求简介 最近这几天做的东西总算是回归咱的老本行了,给投资管理项目做一个台账的东西,就是类似我们的报表.其 中有一个功能是一个高级查询的需求,在查询条件方面大概有7.8个查询条件.需求就是如果一个条件 ...
- Unity合并选中物体的Mesh
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; pu ...
- SVM支持向量机
支持向量机(Support Vector Machine,SVM)是效果最好的分类算法之中的一个. 一.线性分类器: 一个线性分类器就是要在n维的数据空间中找到一个超平面,通过这个超平面能够把两类数据 ...
- Collecting Bugs (概率dp)
Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu ...
- EasyNVR结合阿里云/腾讯云CDN实现微信/小程序直播的方案
背景需求: 许多客户有这样的需求:微信公众号做为平台来对摄像机进行直播:可以让用户随时随地打开公共号就可以观看:保证画面的流畅性:保证视频的并发访问量等. 问题分析: 虽然需求看似很简单,其实真正实现 ...
- GridView 显示行号 设置行号列的宽度
/// <summary> /// GridView 显示行号 设置行号列的宽度 /// </summary> /// <param name="gv" ...
- php建立一个空类: stdClass
$pick = new stdClass; $pick->type = 'full'; ;
- Linux改动hostname的两个办法
假设你想把主机名改为 linux的话.两中方法: 1. # hostname linux 这样改动了以后马上生效.可是重新启动后就没了 2. # vi /etc/sysconfig/network 改 ...
- Java实现微信网页授权
开发前的准备: 1.需要有一个公众号(我这里用的测试号),拿到AppID和AppSecret: 2.进入公众号开发者中心页配置授权回调域名.具体位置:接口权限-网页服务-网页账号-网页授权获取用户基本 ...
- Linux下自动清除MySQL日志文件
MySQL运行过程中会生成大量的日志文件,占用不少空间,修改my.cnf文件配置bin-log过期时间,在Linux下自动清除MySQL日志文件 [mysqld] expire-logs-days= ...