E. Ants in Leaves

题目连接:

http://www.codeforces.com/contest/622/problem/E

Description

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.

Sample Input

12

1 2

1 3

1 4

2 5

2 6

3 7

3 8

3 9

8 10

8 11

8 12

Sample Output

6

Hint

题意

每个叶子都有一个蚂蚁,然后蚂蚁会爬到根去,每秒可以爬一个节点

然后每个节点的蚂蚁最多同时只有一个(除了根

然后问你最少多久,可以使得所有蚂蚁都在根的位置

题解:

贪心就好了

对于叶子,我们都记录下他们的深度,然后我们发现,如果存在两个叶子的深度相同,那么他们一定会相遇在某个点,所以我们只要使得某个点的深度+1就好了

然后这样不断贪心下去就行了

这个可以用桶排解决,反正最多1e6嘛

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
vector<int> E[maxn];
int dep[maxn];
int cnt[maxn];
vector<int> tmp;
void dfs(int x,int fa)
{
if(E[x].size()==1)tmp.push_back(dep[x]);
for(int i=0;i<E[x].size();i++)
{
int v = E[x][i];
if(v==fa)continue;
dep[v]=dep[x]+1;
dfs(v,x);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
//cout<<endl;
int ans = 0;
for(int i=0;i<E[1].size();i++)
{
for(int j=0;j<=E[E[1][i]].size()+5;j++)
cnt[j]=0;
dep[E[1][i]]=1;
tmp.clear();
dfs(E[1][i],1);
sort(tmp.begin(),tmp.end());
int now = 0;
for(int j=0;j<tmp.size();j++)
{
if(now>=tmp[j])now++;
else now=tmp[j];
}
ans=max(ans,now);
}
cout<<ans<<endl;
}

Educational Codeforces Round 7 E. Ants in Leaves 贪心的更多相关文章

  1. Educational Codeforces Round 7 - E. Ants in Leaves

    题目链接:http://www.codeforces.com/contest/622/problem/E 题意是给你一棵树,1为根,每个叶子节点有一个蚂蚁,移动到一个邻接节点时间耗费为1,一个节点上不 ...

  2. Educational Codeforces Round 43 E&976E. Well played! 贪心

    传送门:http://codeforces.com/contest/976/problem/E 参考:https://www.cnblogs.com/void-f/p/8978658.html 题意: ...

  3. C. Brutality Educational Codeforces Round 59 (Rated for Div. 2) 贪心+思维

    C. Brutality time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. Educational Codeforces Round 43 E. Well played!(贪心)

    E. Well played! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Educational Codeforces Round 26

    Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...

  7. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  8. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  9. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

随机推荐

  1. 动软Model 模板 生成可空类型字段

    动软代码 生成可空类型 <#@ template language="c#" HostSpecific="True" #> <#@ outpu ...

  2. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  3. string中的substr() 和 find() 函数

    string问题中经常遇到在stringA中查找stringB,主要通过substr()跟find()来完成 substr().find().replace() 都可以用一个位置加上一个长读去描述子串 ...

  4. AC多模式匹配算法

    建议:学习ac算法最好的途径是看论文pdf_Efficient_String_Matching_An_Aid_to_Biblio 一.一般的搜索算法 keyword: { he, she, his, ...

  5. HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception

    HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception

  6. Netbeans Platform 工程,免安装JDK

    使用Netbeans 6.8 创建了一个Netbeans Platform 工程,以Zip形式发布后, 按照以下操作,可 以在客户端免安装JDK: 1. 从已安装JDK的计算机中,提取JDK:eg. ...

  7. 技术文集:万能WINDOWS XP封装

    这里将系统封装分为3步:做系统.封装.部署 一.做系统 平台不限,但不建议在虚拟机上制作.CPU及主板芯片没有限制,关于intelide和intelppm的不兼容问题,深度白金3in1并没有删除这些注 ...

  8. 字符编解码的故事(ASCII,ANSI,Unicode,Utf-8)

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们认为8个开关状态作为原子单位很好,于是他们把这称为"字节". 再后来,他们又做了一 ...

  9. lbs basic mongodb

    MongoDB地理位置索引常用的有两种. db.places.ensureIndex({'coordinate':'2d'}) db.places.ensureIndex({'coordinate': ...

  10. [添加用户]解决useradd 用户后没有添加用户Home目录的情况,Linux改变文件或目录的访问权限命令,linux修改用户密码

    将nobody用户添加到nogroup 组:usermod -g nogroup nobody cat /etc/passwd|grep nobodynobody:x:65534:65534:nobo ...