Codeforces 685B Kay and Snowflake(树的重心)

来源:互联网 发布:淘宝上买阅读器怎样 编辑:程序博客网 时间:2024/05/17 01:27

题意:

给你一棵树,问你每一颗子树的重心是哪一个节点。

解法:

树的重心有一个性质,当把两棵树合并时,新的树的重心肯定在两颗树重心之间的路径上,所以我们只需要看当前节点到他重儿子重心之间的路径中是否有满足条件的点就可以了,(重儿子请去看树链剖分)因为重儿子是最大的,不在这个路径上找,会让重儿子单独出来,肯定不满足条件。

////  Created by  CQU_CST_WuErli//  Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.////#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define SI(a) scanf("%d", &a)#define SII(a,b) scanf("%d%d", &a, &b)#define SIII(a,b,c) scanf("%d%d%d", &a, &b, &c)const int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7f7f7f7f;const int MOD=1e9+7;const double eps=1e-10;const double pi=acos(-1);typedef long long  ll;using namespace std;const int N = 300030;int n, q;int ans[N];vector<int> g[N];int par[N], son[N];int sz[N];void dfs(int fa, int u) {    par[u] = fa;    sz[u] = 1; son[u] = 0;    ans[u] = u;    for (auto& v : g[u]) {        if (v == fa) continue;        dfs(u, v);        sz[u] += sz[v];        if (!son[u] || sz[son[u]] < sz[v]) son[u] = v;    }    if (sz[son[u]] * 2 > sz[u]) {        int tmp = ans[son[u]];        while ((sz[u] - sz[tmp]) * 2 > sz[u])            tmp = par[tmp];        ans[u] = tmp;    }}int main(int argc, char const *argv[]) {#ifdef LOCAL    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);    // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif    while(SII(n, q) == 2) {        for (int i = 1; i <= n; i++) {            g[i].clear();            ans[i] = -1;        }        for (int u = 2; u <= n; u++) {            int v; SI(v);            g[u].push_back(v);            g[v].push_back(u);        }        dfs(-1, 1);        while (q--) {            int x;            SI(x);            printf("%d\n", ans[x]);        }    }    return 0;}
0 0
原创粉丝点击