Codeforces 622E Ants in Leaves【树型Dp】

E. Ants in Leaves
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Tree is a connected graph without cycles. A leaf of a tree is any vertex connected with exactly one other vertex.

You are given a tree with n vertices and a root in the vertex 1. There is an ant in each leaf of the tree. In one second some ants can simultaneously go to the parent vertex from the vertex they were in. No two ants can be in the same vertex simultaneously except for the root of the tree.

Find the minimal time required for all ants to be in the root of the tree. Note that at start the ants are only in the leaves of the tree.

Input

The first line contains integer n (2 ≤ n ≤ 5·105) — the number of vertices in the tree.

Each of the next n - 1 lines contains two integers xi, yi (1 ≤ xi, yi ≤ n) — the ends of the i-th edge. It is guaranteed that you are given the correct undirected tree.

Output

Print the only integer t — the minimal time required for all ants to be in the root of the tree.

Examples
Input
12
1 2
1 3
1 4
2 5
2 6
3 7
3 8
3 9
8 10
8 11
8 12
Output
6
Input
2
2 1
Output
1

题目大意:


给你一颗具有N个节点的树,规定1作为根。

现在每个叶子节点上都有一只蚂蚁,一秒可以移动到相邻的一个点上。问所有蚂蚁都移动到根最少需要几秒。

现在要求除了根节点以外,每个点都最多只能有一只蚂蚁在上边。


思路(思路源自:http://blog.csdn.net/kirito_acmer/article/details/50682852):


对于贪心方案来讲,我们肯定是想先将距离根节点近的蚂蚁先移动到根节点。

这样做能够避免更多堵塞的情况。

所以我们首先将根节点去掉,那么对应会留下若干子树,答案肯定就是子树中需要最长时间的那个树的时间。

我们拿出所有与1直接相连的点,然后预处理出其子树所有叶子节点的深度dist【i】预处理出来

那么对应深度最低的那个蚂蚁走了之后,同深度的蚂蚁要在到达目的地的时间的基础上+1.

那么就有:dist【i】=max(dist【i-1】+1,dp【i】);满足这个状态转移的情况要求dist【i】此时是有序的。

那么预处理出来dist【i】之后,我们将其按照从小到大排序即可。


Ac代码:

#include
#include
#include
#include
using namespace std;
int dist[6500000];
vectormp[6500000];
int tot;
void Dfs(int u,int from,int depp)
{
    if(mp[u].size()==1)
    {
        dist[tot++]=depp;
    }
    for(int i=0;i








你可能感兴趣的:(搜索,dp,思维,贪心,Codeforces,622E)