zoukankan      html  css  js  c++  java
  • Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心

    题目链接:

    题目

    D. Kay and Snowflake

    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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).

    输入

    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.

    输出

    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.

    样例

    input

    7 4
    1 1 3 3 5 3
    1
    2
    3
    5

    output

    3
    2
    3
    6

    题意

    求需要查询的子树的重心。

    题解

    对于点u,它的重心会在它的以重儿子为根的子树的重心和u的路径上。

    可以用dfs直接暴力递归。(如果u的重儿子子树的重心深度比较高,那么u的重心深度也会比较高,严格的时间证明也不懂。。)

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    
    const int maxn = 3e5 + 10;
    
    int n, m;
    
    int siz[maxn], ans[maxn],f[maxn];
    vector<int> G[maxn];
    void dfs(int u) {
    	siz[u] = 1; ans[u] = u;
    	for (int i = 0; i < G[u].size(); i++) {
    		int v = G[u][i];
    		dfs(v);
    		siz[u] += siz[v];
    	}
    	for (int i = 0; i < G[u].size(); i++) {
    		int v = G[u][i];
    		if (siz[v] * 2 > siz[u]) {
    			ans[u] = ans[v];
    			break;
    		}
    	}
    	while ((siz[u] - siz[ans[u]]) * 2>siz[u]) ans[u] = f[ans[u]];
    }
    
    int main() {
    	scanf("%d%d", &n, &m);
    	for (int i = 2; i <= n; i++) {
    		scanf("%d", f + i);
    		G[f[i]].push_back(i);
    	}
    	dfs(1);
    	while (m--) {
    		int v; scanf("%d", &v);
    		printf("%d
    ", ans[v]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    转载:iOS开发的22个奇谲巧技
    解决pathForResource返回nil, 无法读取plist文件问题
    小项目三:登陆窗口
    小项目一: UIButton的使用
    转载:iOS 8 自适应 Cell
    转载:iOS 8 AutoLayout与Size Class自悟
    转载:总结iOS 8和Xcode 6的各种坑
    [转]Xcode6中如何添加pch文件
    UIButton和UIImageView的区别
    控件的属性
  • 原文地址:https://www.cnblogs.com/fenice/p/5622024.html
Copyright © 2011-2022 走看看