Codeforces Round #359 (Div. 2) D DFS

CrossDolphin發表於2016-07-05



連結:戳這裡


D. Kay and Snowflake
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 vi. 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).

Input
The first line of the input contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the size of the initial tree and the number of queries respectively.

The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ 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 pi define a correct tree.

Each of the following q lines contain a single integer vi (1 ≤ vi ≤ n) — the index of the node, that define the subtree, for which we want to find a centroid.

Output
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.

Example
input
7 4
1 1 3 3 5 3
1
2
3
5
output
3
2
3
6
Note
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.


題意:

給出一棵樹,要求找出節點的中心

這個中心的定義是 對於查詢的節點u。找出u的一顆子樹v,切掉v這顆子樹,使得剩下子樹的大小的最大值都<=原u樹的二分之一。


思路:

查詢u的中心,那麼切除的肯定是size最大的子樹,那麼遞迴子樹的過程我們只需要存下每顆子樹的大小。

再遍歷一遍子樹,存下最大的滿足條件的中點。

但是可能由於中心點還是小了點,那就再往上切一點,直到<=原樹的二分之一為止

(真的無法理解寫程式碼的時候他們竟然在發包子 mdzz

程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int a[500100];
int head[500100];
struct edge{
    int v,next;
}e[600100];
int n,q,tot=0;
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
int num[500100],anw[500100];
void DFS(int u,int fa){
    num[u]=1;
    anw[u]=u;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        DFS(v,u);
        num[u]+=num[v];

    }
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        if(num[v]*2>=num[u]){
            anw[u]=anw[v];
        }
    }
    while((num[u]-num[anw[u]])*2>num[u]){
        anw[u]=a[anw[u]];
    }
}
int main(){
    mst(head,-1);
    scanf("%d%d",&n,&q);
    for(int i=2;i<=n;i++) {
        scanf("%d",&a[i]);
        Add(a[i],i);
        Add(i,a[i]);
    }
    DFS(1,0);
    for(int i=1;i<=q;i++){
        int x;
        scanf("%d",&x);
        printf("%d\n",anw[x]);
    }
    return 0;
}


相關文章