码迷,mamicode.com
首页 > 其他好文 > 详细

启发式合并CodeForces - 1009F

时间:2019-01-21 16:12:18      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:需要   sequence   tor   def   合并   https   const   infinite   thml   

E - Dominant Indices CodeForces - 1009F

You are given a rooted undirected tree consisting of nn vertices. Vertex 11 is the root.

Let‘s denote a depth array of vertex xx as an infinite sequence [dx,0,dx,1,dx,2,][dx,0,dx,1,dx,2,…], where dx,idx,i is the number of vertices yy such that both conditions hold:

  • xx is an ancestor of yy;
  • the simple path from xx to yy traverses exactly ii edges.

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

  • for every k<jk<j, dx,k<dx,jdx,k<dx,j;
  • for every k>jk>j, dx,kdx,jdx,k≤dx,j.

For every vertex in the tree calculate its dominant index.

Input

The first line contains one integer nn (1n1061≤n≤106) — the number of vertices in a tree.

Then n?1n?1 lines follow, each containing two integers xx and yy (1x,yn1≤x,y≤n, xyx≠y). This line denotes an edge of the tree.

It is guaranteed that these edges form a tree.

Output

Output nn numbers. ii-th number should be equal to the dominant index of vertex ii.

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

题意:对于每一个节点x,可以定义一个深度数组[dx0,dx1,dx2,…dxh],代表着以节点x为根往下计算,深度为h的那层的节点的数量。
对于每一个节点x,我们可以从深度数组中,选择一个主要索引下标j,作为他的代表。这个dj需要满足以下条件,他是所有dh中,最大的那个,如果有多个dh是一样的,都是最大的,那么选择j(即深度)最小的那个。
每层节点数的众数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=1e6+5;
int n,m;
int mx,big,id;
int deep[maxn],si[maxn],hson[maxn],cnt[maxn],ans[maxn];
vector<int>G[maxn];

void findhson(int x,int fa,int dep)//找到所有的重儿子
{
    si[x]=1;
    deep[x]=dep;
    int len=G[x].size();
    for(int i=0;i<len;i++)
    {
        int t=G[x][i];
        if(t!=fa)
        {
            findhson(t,x,dep+1);
            si[x]+=si[t];
            deep[t]=deep[x]+1;
            if(si[t]>si[hson[x]])
                hson[x]=t;
        }
    }
}
void cal(int x,int fa,int val)
{
    cnt[deep[x]]+=val;
    if(cnt[deep[x]]>mx)
    {
        id=deep[x];
        mx=cnt[deep[x]];
    }
    else if(cnt[deep[x]]==mx && deep[x]<id)
        id=deep[x];
    int len=G[x].size();
    for(int i=0;i<len;i++)
    {
        int t=G[x][i];
        if(t!=fa && t!=big)
            cal(t,x,val);
    }
}
void dfs(int x,int fa,int flag)
{
    int len=G[x].size();
    for(int i=0;i<len;i++)
    {
        int t=G[x][i];
        if(t!=fa && t!=hson[x])
            dfs(t,x,0);
    }
    if(hson[x])
    {
        dfs(hson[x],x,1);
        big=hson[x];
    }
    cal(x,fa,1);
    big=0;
    ans[x]=id;
    if(!flag)
    {
        cal(x,fa,-1);
        mx=0;id=0;
    }
}
int main()
{
    big=0;mx=0;id=0;
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    findhson(1,0,1);
    dfs(1,0,1);
    for(int i=1;i<=n;i++)
        printf("%d\n",ans[i]-deep[i]);
    return 0;
}

 

启发式合并CodeForces - 1009F

标签:需要   sequence   tor   def   合并   https   const   infinite   thml   

原文地址:https://www.cnblogs.com/jkzr/p/10298352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!