Educational Codeforces Round 7 E. Ants in Leaves 贪心
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 贪心的更多相关文章
- Educational Codeforces Round 7 - E. Ants in Leaves
题目链接:http://www.codeforces.com/contest/622/problem/E 题意是给你一棵树,1为根,每个叶子节点有一个蚂蚁,移动到一个邻接节点时间耗费为1,一个节点上不 ...
- Educational Codeforces Round 43 E&976E. Well played! 贪心
传送门:http://codeforces.com/contest/976/problem/E 参考:https://www.cnblogs.com/void-f/p/8978658.html 题意: ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
随机推荐
- Drupal配置文件settings.php搜索规则
Drupal的配置文件搜索是通过bootstrap.inc的conf_path()函数实现的: function conf_path($require_settings = TRUE, $reset ...
- saltstack配置安装的一些关键步骤及安装时各种报错的分析
以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...
- IGF职业组比赛
IGF职业组比赛 参赛资格: 面向亚太区所有独立游戏开发者(参见详细规则) 截止日期: 2015年7月20日 2015年IGF职业组七大奖项设置如下: * 最佳游戏(RMB20, 000) * 最佳移 ...
- JQuery中的事件以及动画
.bind事件 <script src="script/jquery-1.7.1.min.js"></script> <script> $(fu ...
- 实现JQuery EasyUI右键菜单变灰不可用效果
使用过EasyUI的朋友想必都知道疯狂秀才写的后台界面吧,作为一个初学者我不敢妄自评论它的好坏,不过它确实给我们提供了一个很好框架,只要在它的基础上进行修改,基本上都可以满足我们开发的需要. 知道“疯 ...
- 记录一个 关于 python 普通方法,静态方法和类方法 的介绍。@classmethod @staticmethod
上班时间 只贴看到最厉害的答案 回头总结 http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod ...
- Hadoop构成
What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...
- c++builder Color
procedure ExtractRGB(const Color: Graphics.TColor; out Red, Green, Blue: Byte); var RGB: Windows.TCo ...
- oracle max()函数和min()函数
当需要了解一列中的最大值时,可以使用MAX()函数:同样,当需要了解一列中的最小值时,可以使用MIN()函数.语法如下. SELECT MAX (column_name) / MIN ...
- (转载)javascript函数作用域和提前声明
http://www.cnblogs.com/ArthurPatten/p/3274080.html 一些语言如C.java都有块级作用域,即花括号内的每一段代码都具有各自的作用域,而且变量在声明它们 ...