303998: CF768G. The Winds of Winter

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

The Winds of Winter

题意翻译

给一个有根树。若我们删去一个节点和所有与他相连的边,则会得到一个森林。你希望这个森林中节点最多的树的节点个数尽量少,于是你可以进行至多一次如下操作:删除一个节点和其父亲之间的边,把这个节点连到某个节点上。这个操作不得改变森林中树的个数。 对于每个节点,输出它作为删去的节点时,进行至多一次操作后的最大树大小的最小值。 输入格式中,一条边的一个端点是0说明另一个端点是根。

题目描述

Given a rooted tree with $ n $ nodes. The Night King removes exactly one node from the tree and all the edges associated with it. Doing this splits the tree and forms a forest. The node which is removed is not a part of the forest. The root of a tree in the forest is the node in that tree which does not have a parent. We define the strength of the forest as the size of largest tree in forest. Jon Snow wants to minimize the strength of the forest. To do this he can perform the following operation at most once. He removes the edge between a node and its parent and inserts a new edge between this node and any other node in forest such that the total number of trees in forest remain same. For each node $ v $ you need to find the minimum value of strength of the forest formed when node $ v $ is removed.

输入输出格式

输入格式


The first line of the input contains an integer $ n $ ( $ 1<=n<=10^{5} $ ) — the number of vertices in the tree. Each of the next $ n $ lines contains a pair of vertex indices $ u_{i} $ and $ v_{i} $ ( $ 1<=u_{i},v_{i}<=n $ ) where $ u_{i} $ is the parent of $ v_{i} $ . If $ u_{i}=0 $ then $ v_{i} $ is the root.

输出格式


Print $ n $ line each containing a single integer. The $ i $ -th of them should be equal to minimum value of strength of forest formed when $ i $ -th node is removed and Jon Snow performs the operation described above at most once.

输入输出样例

输入样例 #1

10
0 1
1 2
1 3
1 4
2 5
2 6
3 7
4 8
4 9
5 10

输出样例 #1

3
4
5
5
5
9
9
9
9
9

输入样例 #2

2
2 1
0 2

输出样例 #2

1
1

说明

The tree for first test case is depicted below. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF768G/92e83d84eb7fd603b83b72b75cbb259bd68c6078.png) When you remove the first node, the tree splits to form the following forest. The strength of this forest is $ 4 $ . ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF768G/579396fe74309f35769274fb1d696feecaa9b72f.png) Jon Snow now changes the parent of vertex $ 10 $ from $ 5 $ to $ 3 $ . The strength of forest now becomes $ 3 $ . ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF768G/6a62e865869bd9e74df710a3e1a2ddb4d1a90d68.png)

Input

题意翻译

给一个有根树。若我们删去一个节点和所有与他相连的边,则会得到一个森林。你希望这个森林中节点最多的树的节点个数尽量少,于是你可以进行至多一次如下操作:删除一个节点和其父亲之间的边,把这个节点连到某个节点上。这个操作不得改变森林中树的个数。 对于每个节点,输出它作为删去的节点时,进行至多一次操作后的最大树大小的最小值。 输入格式中,一条边的一个端点是0说明另一个端点是根。

加入题单

上一题 下一题 算法标签: