A10391 | Kay and Snowflake
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.
Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of $n$ nodes. The root of tree has index $1$ . Kay is very interested in the structure of this tree.
After doing some research he formed $q$ queries he is interested in. The $i$ -th query asks to find a centroid of the subtree of the node $v_{i}$ . Your goal is to answer all queries.
Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node $v$ is formed by nodes $u$ , such that node $v$ is present on the path from $u$ to root.
Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).
Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of $n$ nodes. The root of tree has index $1$ . Kay is very interested in the structure of this tree.
After doing some research he formed $q$ queries he is interested in. The $i$ -th query asks to find a centroid of the subtree of the node $v_{i}$ . Your goal is to answer all queries.
Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node $v$ is formed by nodes $u$ , such that node $v$ is present on the path from $u$ to root.
Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).
输入格式
The first line of the input contains two integers $n$ and $q$ ( $2<=n<=300000$ , $1<=q<=300000$ ) — the size of the initial tree and the number of queries respectively.
The second line contains $n-1$ integer $p_{2},p_{3},...,p_{n}$ ( $1<=p_{i}<=n$ ) — the indices of the parents of the nodes from $2$ to $n$ . Node $1$ is a root of the tree. It's guaranteed that $p_{i}$ define a correct tree.
Each of the following $q$ lines contain a single integer $v_{i}$ ( $1<=v_{i}<=n$ ) — the index of the node, that define the subtree, for which we want to find a centroid.
The second line contains $n-1$ integer $p_{2},p_{3},...,p_{n}$ ( $1<=p_{i}<=n$ ) — the indices of the parents of the nodes from $2$ to $n$ . Node $1$ is a root of the tree. It's guaranteed that $p_{i}$ define a correct tree.
Each of the following $q$ lines contain a single integer $v_{i}$ ( $1<=v_{i}<=n$ ) — the index of the node, that define the subtree, for which we want to find a centroid.
输出格式
For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.
输入输出样例
输入 #1
7 4 1 1 3 3 5 3 1 2 3 5
输出 #1
3 2 3 6
The first query asks for a centroid of the whole tree — this is node $3$ . If we delete node $3$ the tree will split in four components, two of size $1$ and two of size $2$ .
The subtree of the second node consists of this node only, so the answer is $2$ .
Node $3$ is centroid of its own subtree.
The centroids of the subtree of the node $5$ are nodes $5$ and $6$ — both answers are considered correct.
The subtree of the second node consists of this node only, so the answer is $2$ .
Node $3$ is centroid of its own subtree.
The centroids of the subtree of the node $5$ are nodes $5$ and $6$ — both answers are considered correct.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted