1. 程式人生 > >F. Dominant Indices Educational Codeforces Round 47 (Rated for Div. 2)(樹上啟發式合併)

F. Dominant Indices Educational Codeforces Round 47 (Rated for Div. 2)(樹上啟發式合併)

F. Dominant Indices
time limit per test
4.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a rooted undirected tree consisting of n
vertices. Vertex 1

is the root.

Let’s denote a depth array of vertex x
as an infinite sequence [dx,0,dx,1,dx,2,…], where dx,i is the number of vertices y

such that both conditions hold:

x

is an ancestor of y
;
the simple path from x
to y traverses exactly i

edges. 

The dominant index of a depth array of vertex x
(or, shortly, the dominant index of vertex x) is an index j

such that:

for every k<j

, dx,k

#include<bits/stdc++.h>
#define LL long long #define INF 0x3f3f3f3f #define lson rt<<1 #define rson rt<<1|1 using namespace std; const int maxn = 1e6+6; int siz[maxn]; int son[maxn]; int skip[maxn]; int n; int dep[maxn]; int head[maxn]; int tot; LL ans[maxn]; LL cnt[maxn]; struct node { int u,v; int net; }E[maxn*2
]; void init() { memset(head,-1,sizeof(head)); tot = 0; } void build(int u,int v) { E[tot].u = u; E[tot].v = v; E[tot].net = head[u]; head[u] = tot++; } void dfs0(int u,int fa,int deep) { siz[u] = 1; dep[u] = deep; for(int i = head[u];~i;i = E[i].net){ int v = E[i].v; if(v==fa){ continue; } dfs0(v,u,deep+1); siz[u]+=siz[v]; if(siz[son[u]]<siz[v]){ son[u] = v; } } } LL mx = 0; LL sum = 0; void getans(int u,int fa,int vv,int uu) { //cout<<uu<<' '<<u<<endl; cnt[dep[u]]+=vv; if(vv>0&&cnt[dep[u]]>mx){ mx = cnt[dep[u]]; sum = dep[u]; } else if(vv>0&&cnt[dep[u]]==mx){ sum = min(sum,(LL)dep[u]); } for(int i = head[u];~i;i = E[i].net){ if(E[i].v==fa||skip[E[i].v]) continue; getans(E[i].v,u,vv,uu); } } void dfs(int u,int fa,int op) { // cout<<u<<endl; for(int i = head[u];~i;i = E[i].net){ int v = E[i].v; if(v==fa||v==son[u]) continue; dfs(v,u,0); } if(son[u]){ dfs(son[u],u,1); skip[son[u]] = 1; } getans(u,fa,1,u); ans[u] = sum-dep[u]; if(son[u]){ skip[son[u]] = 0; } if(op==0){ getans(u,fa,-1,u); sum = mx = 0; } } int main() { init(); scanf("%d",&n); for(int i = 0;i<n-1;i++){ int u,v; scanf("%d%d",&u,&v); build(u,v); build(v,u); } dfs0(1,0,1); dfs(1,0,1); for(int i = 1;i<=n;i++){ printf("%lld\n",ans[i]); } }