题目

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. 【openCV学习笔记】在Mac上配置openCV步骤详解
  2. CMD 控制台CMD程序怎么执行完不关闭界面?@pause
  3. boost::mpl模块实现transform_view相关的测试程序
  4. c++I/O流的概念和流类库的结构
  5. Nginx 简单的cpu配置
  6. HTTP状态保持(cookie、session)
  7. 影片模块 Dubbo 服务聚合
  8. python获取列表序号_确定列表中的序列号(Python)
  9. Outlook 2003解除附件下载限制
  10. 驱动模块的安装与卸载指令
  11. linux汉诺塔实验报告,汉诺塔问题实验报告
  12. 如何用python计算行列式_Python入门教程: 计算范德蒙矩阵的行列式
  13. 简单多边形的三角剖分相关技术
  14. 高等数学学习笔记——第六十讲——向量值函数的导数与积分
  15. 结构化思维(Structured Thinking)
  16. inux常用命令-持续更新中(转载:小牛导航,super-nb)
  17. 手动部署OpenStack之环境部署
  18. Numpy 计算男女生各科成绩统计指标
  19. UI自动化测试是什么?什么项目适合做UI自动化测试
  20. 年轻代、年老代和持久代

热门文章

  1. mysql 上一周起始时间_mysql 某周的起始和结束日期
  2. 怎么大量转换图片格式为tiff
  3. 全球及中国触屏控制器芯片行业研究及十四五规划分析报告
  4. win10安装打印机操作无法完成0x0000007e找不到指定模块
  5. XP系统最大能支持多少内存
  6. EINT DINT ERTM DRTM EALLOW EDIS ESTOP0的理解
  7. 2019 Multi-University Training Contest 2:Beauty Of Unimodal Sequence(DP + 贪心构造)
  8. UFS开发板代码分析
  9. java飞扬的小鸟,三国战纪,网络飞车游戏知识总结
  10. 秒懂,Java 注解 (Annotation)你可以这样学