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,k≤dx,jdx,k≤dx,j.

For every vertex in the tree calculate its dominant index.

Input

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

Then n−1n−1 lines follow, each containing two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠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
题解:题目意思为:给你一颗有根数,对于每一个节点,他的子树到他的边数为d,到达他的边数为d的数量为fd,让你求每一个节点fd最大的d,如果有多个选择d最小的那个;
用map<int,int> mp[i][d]:表示节点I的子树中的节点中深度为d(表示到根节点的边数)的数量。然后沿着重边一直往下搜索,启发式搜索,每次更新ans[u]即可;
参考代码:
 #include<bits/stdc++.h>
using namespace std;
#define clr(a,val) memset(a,val,sizeof(a))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int maxn=1e6+;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
struct Edge{
int to,nxt;
} edge[maxn<<];
int n,cnt,dep[maxn],hvy[maxn],hson[maxn],head[maxn<<];
int maxd[maxn],ans[maxn];
map<int,int> mp[maxn];
inline void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
inline void dfs1(int u,int fa)
{
hvy[u]=dep[u];
for(int e=head[u];~e;e=edge[e].nxt)
{
int v=edge[e].to;
if(v==fa) continue;
dep[v]=dep[u]+;
dfs1(v,u);
if(hvy[v]>hvy[u])
{
hson[u]=v;
hvy[u]=hvy[v];//子树的最深深度
}
}
}
inline void dfs2(int u,int fa)
{
for(int e=head[u];~e;e=edge[e].nxt)
{
int v=edge[e].to;
if(v==fa) continue;
dfs2(v,u);
}
if(hson[u]>)
{
int v=hson[u];
swap(mp[u],mp[v]);
mp[u][dep[u]]=;
if(maxd[v]>)
{
maxd[u]=maxd[v];
ans[u]=ans[v]+;
}
else maxd[u]=,ans[u]=;
}
else maxd[u]=,ans[u]=,mp[u][dep[u]]=;
map<int,int>::iterator it;
for(int i=head[u];~i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==fa || v==hson[u]) continue;
for(it=mp[v].begin();it!=mp[v].end();it++)
{
mp[u][(*it).fi]+=mp[v][(*it).fi];
if(mp[u][(*it).fi]>maxd[u])
{
maxd[u]=mp[u][(*it).fi];
ans[u]=(*it).fi-dep[u];
}
if(mp[u][(*it).fi]==maxd[u] && (*it).fi-dep[u]<ans[u]) ans[u]=(*it).fi-dep[u];
}
}
} int main()
{
n=read();int u,v;
cnt=;clr(head,-);
for(int i=;i<=n;++i) mp[i].clear();
for(int i=;i<n;++i)
{
u=read(),v=read();
addedge(u,v);addedge(v,u);
}
dep[]=;dfs1(,);dfs2(,);
for(int i=;i<=n;++i) printf("%d%c",ans[i],i==n? '\n':' ');
return ;
}

CF1009F Dominant Indices(启发式合并)的更多相关文章

  1. [CF1009F] Dominant Indices (+dsu on tree详解)

    这道题用到了dsu(Disjoint Set Union) on tree,树上启发式合并. 先看了CF的官方英文题解,又看了看zwz大佬的题解,差不多理解了dsu on tree的算法. 但是时间复 ...

  2. CF1009F Dominant Indices 解题报告

    CF1009F Dominant Indices 题意简述 给出一颗以\(1\)为跟的有根树,定义\(d_{i,j}\)为以\(i\)为根节点的子树中到\(i\)的距离恰好为\(j\)的点的个数,对每 ...

  3. CF1009F Dominant Indices

    传送门 还是放个链接让泥萌去学一下把 orzYYB 题目中要求的\(f_{x,j}\),转移是\(f_{x,j}=\sum_{y=son_x} f_{y,j-1}\),所以这个东西可以用长链剖分优化, ...

  4. CF1009F Dominant Indices——长链剖分优化DP

    原题链接 \(EDU\)出一道长链剖分优化\(dp\)裸题? 简化版题意 问你每个点的子树中与它距离为多少的点的数量最多,如果有多解,最小化距离 思路 方法1. 用\(dsu\ on\ tree\)做 ...

  5. CF1009F Dominant Indices(树上DSU/长链剖分)

    题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...

  6. CF1009F Dominant Indices 长链剖分

    题目传送门 https://codeforces.com/contest/1009/problem/F 题解 长链剖分的板子吧. 令 \(dp[x][i]\) 表示 \(x\) 的子树中的深度为 \( ...

  7. Codeforces 1009 F. Dominant Indices(长链剖分/树上启发式合并)

    F. Dominant Indices 题意: 给一颗无向树,根为1.对于每个节点,求其子树中,哪个距离下的节点数量最多.数量相同时,取较小的那个距离. 题目: 这类题一般的做法是树上的启发式合并,复 ...

  8. 【CF1009F】Dominant Indices(长链剖分)

    [CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...

  9. Codeforces 1009 F - Dominant Indices

    F - Dominant Indices 思路:树上启发式合并 先跑轻子树,然后清除轻子树的信息 最后跑重子树,不清除信息 然后再跑一遍轻子树,重新加回轻子树的信息 由于一个节点到根节点最多有logn ...

随机推荐

  1. Servlet中response的相关案例(重定型,验证码,ServletContext文件下载)

    重定向 首先设置状态码,设置响应头 //访问Demo1自动跳转至Demo2 //设置状态码 response.setStatus(302); //设置响应头 response.setHeader(&q ...

  2. 《计算机网络 自顶向下方法》 第3章 运输层 Part2

    待补充完善 TCP 相关基本点 1.面向连接 两个不同主机上的进程在通过 TCP 进行通信之前,必须先通过三次握手来建立 TCP 连接 2.全双工服务 即,如果一台主机上的进程 A 与另一台主机上的进 ...

  3. PHP 性能优化 - php.ini 配置

    内存 默认设置 memory_limit = 128M 单个进程可使用的内存最大值,这个值的设定可以从以下几点考虑: 应用的类型.如果是内存集中型应用,可增加该值: 单个 PHP 进程平均消耗的内存, ...

  4. cn_windows虚拟机配置

    1.打开“VMware”,点击“主页”,点“创建新的虚拟机”: 2.会弹出一个“新建虚拟机向导”,类型选择“典型”,点击“下一步”: 3.选择“稍后安装操作系统”,点击“下一步”: 4.选择“Micr ...

  5. Session,Token,Cookie相关区别

    1. 为什么要有session的出现? 答:是由于网络中http协议造成的,因为http本身是无状态协议,这样,无法确定你的本次请求和上次请求是不是你发送的.如果要进行类似论坛登陆相关的操作,就实现不 ...

  6. 纯css实现tab导航

    仿照这个 实现了一个纯css的导航功能 html <div class="main"> <div id="contain1">列表一内容 ...

  7. nginx(二):基本应用

    配置文件详解 event段配置 worker_connections #; 每个worker进程所能够响应的最大并发请求数量: nginx最大并发响应数=worker_proceses * worke ...

  8. linux shell编程之变量和bash配置文件(第一篇)

    编程语言有两类 强类型:如C语言.数据具有其特定的类型,先声明定义后才能使用.数据运算时必须符合类型要求(如不能把字符串类型数据直接与整型数据做算数运算) 弱类型:如shell.数据默认为字符型,不用 ...

  9. python字符串删除,列表删除以及字典删除的总结

    一:字符串删除  1,字符串本身是不可变的,一个字符串定义以后,对他本身是不能做任何操作的,所以的增删改都是对原字符串拷贝的副本的操作,原来的字符串还是原来的字符串,它本身并没 有变 2,字符串本身是 ...

  10. JDK动态代理和CGLIB字节码增强

    一.JDK动态代理 Java 在 java.lang.reflect 包中有自己的代理支持,该类(Proxy.java)用于动态生成代理类,只需传入目标接口.目标接口的类加载器以及 Invocatio ...