「CF1109F」Dominant Indices【树上启发式合并】

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 n n vertices. Vertex 1 1 1 is the root.

Let’s denote a depth array of vertex x x x as an infinite sequence [ d x , 0 , d x , 1 , d x , 2 , … ] , [d_{x,0},d_{x,1},d_{x,2},…], [dx,0,dx,1,dx,2,], where d x , i d_{x,i} dx,i is the number of vertices y y y such that both conditions hold:

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

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

  • for every k < j , d x , k < d x , j k<j, d_{x,k}<d_{x,j} k<j,dx,k<dx,j;
  • for every k > j , d x , k ≤ d x , j . k>j, d_{x,k}\leq d_{x,j}. k>j,dx,kdx,j.

For every vertex in the tree calculate its dominant index.

Input

The first line contains one integer n   ( 1 ≤ n ≤ 1 0 6 ) n\ (1\leq n\leq 10^6) n (1n106) — the number of vertices in a tree.

Then n − 1 n−1 n1 lines follow, each containing two integers x x x and y y y ( 1 ≤ x , y ≤ n , x ! = y ) . (1\leq x,y\leq n, x!=y). (1x,yn,x!=y). This line denotes an edge of the tree.

It is guaranteed that these edges form a tree.

Output

Output n n n numbers. i − t h i-th ith number should be equal to the dominant index of vertex i i i.

Examples

input
4
1 2
2 3
3 4
output
0
0
0
0
input
4
1 2
1 3
1 4
output
1
0
0
0
input
4
1 2
2 3
2 4
output
2
1
0
0

题意

  • 就是给你一颗有根树,对于每一颗子树,统计每一个深度下的节点数,求有最多节点的深度 h h h,如果节点数有相同的选深度小的那个

题解

  • 树上启发式合并模板题
  • 对于相同节点的深度,可以直接用 1 0 6 10^6 106 s e t set set去维护节点数为 i i i的所有深度,然后就能方便的去撤销子树的信息
  • 复杂度 n ( log ⁡ n ) 2 n(\log n)^2 n(logn)2 4.5 s 4.5s 4.5s刚刚好

代码

#include<bits/stdc++.h>
 
using namespace std;
const int maxn=2e6+10;

//题目数据
int t,n,u,v,q,k,a[maxn],b[maxn],ans[maxn],cnt[maxn],sum,maxx=0,he=0,cnth[maxn],pos[maxn];
vector<int> vec[maxn];

//树剖用
int tot=0,siz[maxn],son[maxn];
int vis[maxn];

void dfs1(int cur,int fath,int he){ //dfs(root,0,1)
    siz[cur]=1;
    for(int i=0;i<vec[cur].size();i++){
        if(vec[cur][i]!=fath){
            dfs1(vec[cur][i],cur,he+1);
            siz[cur]+=siz[vec[cur][i]];
            if(siz[vec[cur][i]]>siz[son[cur]]) son[cur]=vec[cur][i];
        }
    }
}

void calc(int cur,int fa,int val,int h)
{
    if(val==1) {
        if(cnth[h]) pos[cnth[h]].erase(h);
        cnth[h]++;
        pos[cnth[h]].insert(h);
        if(cnth[h]>maxx) {maxx=cnth[h];he=(*pos[cnth[h]].begin());}
        else if(cnth[h]==maxx) he=(*pos[cnth[h]].begin());
    }else {
        pos[cnth[h]].erase(h);
        cnth[h]--;
        pos[cnth[h]].insert(h);
        if(!pos[cnth[h]+1].size()&&maxx==cnth[h]+1) {
            maxx=cnth[h];
            he=(*pos[cnth[h]].begin());
        }else if(maxx==cnth[h]) {
            he=(*pos[cnth[h]].begin());
        }
    }
    for(int i=0;i<vec[cur].size();i++){
        if(vec[cur][i]!=fa&&!vis[vec[cur][i]]){  
            calc(vec[cur][i],cur,val,h+1);
        }
    }
}

void dfs(int cur,int fa,bool keep,int h) 
{
    for(int i=0;i<vec[cur].size();i++){
        if(vec[cur][i]!=fa&&vec[cur][i]!=son[cur]){
            dfs(vec[cur][i],cur,0,h+1);
        }
    }
    if(son[cur]) dfs(son[cur],cur,1,h+1),vis[son[cur]]=1; 
    calc(cur,fa,1,h);
    ans[cur]=he-h; 
    if(son[cur]) vis[son[cur]]=0;  
    if(!keep) calc(cur,fa,-1,h);  
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        scanf("%d %d",&u,&v);
        vec[u].push_back(v);
        ec[v].push_back(u);
    }
    dfs1(1,0,1);
    dfs(1,0,0,1);
    for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值