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. 防范 DDoS 攻击的 15 个方法

    为了对抗 DDoS(分布式拒绝服务)攻击,你需要对攻击时发生了什么有一个清楚的理解. 简单来讲,DDoS 攻击可以通过利用服务器上的漏洞,或者消耗服务器上的资源(例如 内存.硬盘等等)来达到目的.DD ...

  2. java 判断两个时间段是不是有交集

    如上图:X Y  Z  分别为传来的开始时间可能位于数据库中时间段的位置. X有三种可能 即传来的开始时间为与数据可中某条数据的开始位置!      这样他的结束时间就有三种可能       1.位于 ...

  3. Java并发编程-关卡

    CyclicBarrier 直译过来叫循环栅栏,它主要的方法就是一个:await().await() 方法没被调用一次,计数便会减少1,并阻塞住当前线程.当计数减至0时,阻塞解除,所有在此 Cycli ...

  4. cocos2d-x CocoStudio中场景触发器(Trigger)的使用和扩展

    场景编辑器中的触发器是一种通过事件触发机制,在特定的事件被触发的时候自动执行自己预先定义的动作或者功能. 编辑器中带有一些默认的事件.条件和动作,当然也可以扩展自定义的事件.条件和动作. 触发器可以创 ...

  5. Beginner’s Guide(开始者向导)

    This guide gives a basic introduction to nginx and describes some simple tasks that can be done with ...

  6. TLCL中英对照版

    TLCL中英文对照阅读网址:http://billie66.github.io/TLCL/book/index.html 感谢好奇猫团队(http://haoqicat.com/about/team) ...

  7. Python【基础第三篇】

    set集合 s1=set() 集合特点: 访问速度快 没有重复项 collections系列(数据类型容器模块) 一.计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数. ...

  8. Camera拍照声设定

    在某些国家(比如Japan),为了防止偷拍,强制拍照声是需要从Speaker出来的(即使插入耳机的情况下). 实现该功能比较简单的方法就是将拍照声类型设置为Ringtone 或 Alarm 或 Not ...

  9. linux 新学到的命令

    nohup python -u /data/daemon/daemon/run.py & 使py程序可以在后台运行 nohup php a.php & 在linux平台上,要在后台运行 ...

  10. 面试题收集——Java基础部分(一)

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3641769.html 感谢! 1.一个".java"源文件中是否可以包括多个类(不是内部类) ...