题目

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<dx,j;
for every k>j, dx,k≤dx,j.
For every vertex in the tree calculate its dominant index.

Input

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

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

It is guaranteed that these edges form a tree.

Output

Output n numbers. i-th number should be equal to the dominant index of vertex 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

思路

和上面一道题目差不多,都是树上启发式合并,就是统计一下深度就好了啦~

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mx=1e6+10;
int n,head[mx],tot;
int size[mx],deep[mx],son[mx];
int cnt[mx],nowval,maxval,vis[mx],ans[mx];struct node{int to,next;
}e[mx<<1];void init()
{tot=0;memset(head,-1,sizeof(head));memset(vis,0,sizeof(vis));
}void add(int u,int v)
{e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
}void dfs(int u,int fa,int d)
{size[u]=1;deep[u]=d;for(int i=head[u];i!=-1;i=e[i].next){int v=e[i].to;if(v==fa) continue;dfs(v,u,d+1);size[u]+=size[v];if(size[v]>size[son[u]])son[u]=v;}
}void update(int u,int fa,int k)
{cnt[deep[u]]+=k;if(k>0&&cnt[deep[u]]>=maxval){if(cnt[deep[u]]>maxval) maxval=cnt[deep[u]],nowval=deep[u];else if(cnt[deep[u]]==maxval&&deep[u]<nowval)nowval=deep[u];}for(int i=head[u];i!=-1;i=e[i].next){int v=e[i].to;if(v!=fa&&!vis[v])update(v,u,k);}
}void dsu(int u,int fa,int keep)
{for(int i=head[u];i!=-1;i=e[i].next){int v=e[i].to;if(v!=fa&&v!=son[u])//优先轻儿子dsu(v,u,0);}if(son[u]) dsu(son[u],u,1),vis[son[u]]=1;update(u,fa,1);ans[u]=nowval-deep[u];if(son[u]) vis[son[u]]=0;if(!keep) update(u,fa,-1),nowval=maxval=0;
}int main()
{init();scanf("%d",&n);for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);add(u,v);add(v,u);}dfs(1,0,1);dsu(1,0,1);for(int i=1;i<=n;i++)printf("%d\n",ans[i]);return 0;
}

Dominant Indices相关推荐

  1. cf1009F. Dominant Indices

    cf1009F. Dominant Indices 题意: 1号节点为根,问对于以每个点为根的子树种,求某个深度节点最多的层数.如果多个相等,输出深度小的. 题解: 直接dsu就完事了,相当于是求子树 ...

  2. 【luogu CF1009F】Dominant Indices(长链剖分优化DP)

    Dominant Indices 题目链接:luogu CF1009F 题目大意 给你一棵以 1 为根的树,设 d(u,x) 为 u 子树总到 u 距离为 x 的点数. 然后要你对于每个点 x 都要求 ...

  3. 【Cf Edu #47 F】Dominant Indices(长链剖分)

    要求每个点子树中节点最多的层数,一个通常的思路是树上启发式合并,对于每一个点,保留它的重儿子的贡献,暴力扫轻儿子将他们的贡献合并到重儿子里来. 参考重链剖分,由于一个点向上最多只有$log$条轻边,故 ...

  4. Dominant Indices(CF 1009 F)

    前言 记录一下长链剖分的小技巧 题目相关 链接 题目大意 一棵nnn个节点的树,定义fi,jf_{i,j}fi,j​为与iii号点距离为jjj的节点数量,对于每个iii求出一个最小的jjj满足fi,j ...

  5. 【CF1009F】 Dominant Indices (长链剖分+DP)

    题目链接 \(O(n^2)\)的\(DP\)很容易想,\(f[u][i]\)表示在\(u\)的子树中距离\(u\)为\(i\)的点的个数,则\(f[u][i]=\sum f[v][i-1]\) 长链剖 ...

  6. Educational Codeforces Round 47 (Div 2) (A~G)

    目录 Codeforces 1009 A.Game Shopping B.Minimum Ternary String C.Annoying Present D.Relatively Prime Gr ...

  7. dsu on tree 题集 + ac代码

    文章目录 **入门讲解** **[600E - Lomsat gelral](https://codeforces.ml/problemset/problem/600/E)** **[树上数颜色](h ...

  8. [dsu on tree]树上启发式合并总结(算法思想及模板附例题练习)

    文章目录 前言 树上启发式合并 引入 算法思想 时间复杂度 模板 练习 例题:CF600E Lomsat gelral solution code CF208E Blood Cousins solut ...

  9. 有趣题目和认知合集(持续更新)

    写写对一些算法的理解,挂几个有意思的题,可能也会挂几个板子题 算法理解偏向于能懂即可,没有严格的证明 快乐几何 [1.2]Volatile Kite 点到直线 快乐搜与暴力 [2.4]Short Co ...

  10. ZJOI2019一轮停课刷题记录

    Preface 菜鸡HL终于狗来了他的省选停课,这次的时间很长,暂定停到一试结束,不过有机会二试的话还是可以搞到4月了 这段时间的学习就变得量大而且杂了,一般以刷薄弱的知识点和补一些新的奇怪技巧为主. ...

最新文章

  1. 程序员面试题精选100题(22)-整数二进制表示中1的个数[算法]
  2. 第一百四十五节,JavaScript,同步动画
  3. android 字符串特殊字符转义
  4. 对软件研发项目管理的深入探讨
  5. 解决debian (Friendly ARM 嵌入式板)的sudo等一部分命令无法TAB补全
  6. Chrome 跨域调试
  7. 组装多媒体计算机需要什么硬件,多媒体计算机组装与维护教程 第1章 多媒体计算机硬件选购和配置...
  8. 《程序员修炼之道——从小工到专家》 读书笔记
  9. Arduino驱动MAX30102踩坑记
  10. GBASE 8s UDR内存管理_03_mi_realloc
  11. cesium 漫游飞行_cesium之三维漫游飞行效果实现篇
  12. 关于BIOS加载BOOT.S的经典解答
  13. hue-登录相关-简
  14. linux vim 命令无效,Linux vim 命令 command not found vim 命令详解 vim 命令未找到 vim 命令安装 - CommandNotFound ⚡️ 坑否...
  15. bm17bm6bm18
  16. wget linux
  17. 解决pycharm调用plt.show()后无图片显示问题
  18. 计算机组装实验老毛桃u盘报告,老毛桃WINPE优盘(U盘)安装系统图解
  19. bilibiliC++概念遍览
  20. SwiftUI 音乐类App之Tuner App 调谐器适用于任何乐器支持木风 黄铜 琴弦 声音

热门文章

  1. BIOS中英文对照表!
  2. raw格式怎么打开转换成jpg?迅捷图片转换器3步搞定
  3. 方差公式初三_九年级同步数学公式:方差公式(1)
  4. 巅峰战舰 服务器维护,《巅峰战舰》停止充值关闭服务器公告
  5. Font Awesome 找图标的正确姿势
  6. 面试经常考的五个Sql查询
  7. 【人脸58点关键点】基于面积对比法的脑卒中识别算法
  8. 湖北飞young使用任意路由器教程
  9. 大数据对于企业的价值,主要体现在哪几个方面?
  10. foxmail代理设置方法