Codeforces 686 D - Kay and Snowflake

D - Kay and Snowflake

思路:

树的重心

利用重心的一个推论,树的重心必定在子树重心的连线上。

然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心一定在这颗子树上。

利用以上两条,可知:

如果没有一颗子树的大小超过整棵树的大小的1/2,那么就可以以根节点为树的重心,

不然就从 超过1/2大小的子树 的重心 到 根节点 之间找整棵树的重心。

因为不会找重复的点,所以退化的复杂度为O(n)

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define LL long long 
#define pb push_back
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a)) 

const int N = 3e5 + 5;
vector<int>g[N];
int tot[N], ans[N], fa[N];
void dfs(int u) {
    tot[u] = 1;
    for (int i = 0; i < g[u].size(); i++) {
        dfs(g[u][i]);
        tot[u] += tot[g[u][i]];
    }    
    ans[u] = u;
    for (int i = 0; i < g[u].size(); i++) {
        if(tot[g[u][i]] * 2 > tot[u]) ans[u] = ans[g[u][i]];
    }
    while((tot[u] - tot[ans[u]]) * 2 > tot[u]) ans[u] = fa[ans[u]];
}
int main() {
    int n, u, q;
    scanf("%d%d", &n, &q);
    for (int i = 2; i <= n; i++) {
        scanf("%d", &fa[i]);
        g[fa[i]].pb(i);
    }
    dfs(1);
    while (q--) {
        scanf("%d", &u);
        printf("%d\n",ans[u]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/widsom/p/8918581.html
今日推荐