题目链接:

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

题意:

除根节点外每个节点只能容纳一只蚂蚁,现在每个叶子节点都有一个蚂蚁,问这些蚂蚁都到达根节点的最短时间是多少;

思路:

贪心,深度小的蚂蚁一定要先走才能保证时间最短,所有按深度排序后每个叶子节点的蚂蚁到达根节点的时间就是上一个节点的最大时间+1与其深度的相比的较大值,即ans[i]=max(ans[i-1]+1,dep[i]);

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=5e5+10;
const int maxn=(1<<20)+14;
const double eps=1e-12; int n,cnt;
vector<int>ve[N];
int dep[N],num[N];
void dfs(int cur,int fa,int deep)
{
int len=ve[cur].size();
for(int i=0;i<len;i++)
{
int x=ve[cur][i];
if(x==fa)continue;
dfs(x,cur,deep+1);
}
if(len==1)dep[++cnt]=deep;
}
int main()
{
int u,v;
read(n);
For(i,1,n-1)
{
read(u);read(v);
ve[u].push_back(v);
ve[v].push_back(u);
}
int len=ve[1].size(),ans=0;
for(int i=0;i<len;i++)
{
cnt=0;
dfs(ve[1][i],1,1);
sort(dep+1,dep+cnt+1);
for(int j=1;j<=cnt;j++)
{
num[j]=max(num[j-1]+1,dep[j]);
}
ans=max(ans,num[cnt]);
}
//for(int i=0;i<len;i++)ans=max(ans,);
print(ans);
return 0;
}

  


codeforces 622E E. Ants in Leaves(贪心+dfs)的更多相关文章

  1. Educational Codeforces Round 7 E. Ants in Leaves 贪心

    E. Ants in Leaves 题目连接: http://www.codeforces.com/contest/622/problem/E Description Tree is a connec ...

  2. 小黑的镇魂曲(HDU2155:贪心+dfs+奇葩解法)

    题目:点这里 题目的意思跟所谓的是英雄就下100层一个意思……在T秒内能够下到地面,就可以了(还有一个板与板之间不能超过H高). 接触这题目是在昨晚的训练赛,当时拍拍地打了个贪心+dfs,果断跟我想的 ...

  3. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  4. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  5. 【bzoj3252】攻略 贪心+DFS序+线段树

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...

  6. hdu6060[贪心+dfs] 2017多校3

    /* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...

  7. codeforces 622E. Ants in Leaves

    题目链接 给一棵有根树, 每个叶子节点上有一只蚂蚁. 在0时刻蚂蚁开始向上爬, 同一时刻, 除了根节点以外, 一个节点上面不能有2个蚂蚁. 问所有的蚂蚁都爬到根节点需要的最短时间. 因为除了根节点, ...

  8. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

  9. codeforces622E Ants in Leaves (dfs)

    Description Tree is a connected graph without cycles. A leaf of a tree is any vertex connected with ...

随机推荐

  1. 检验 java 基础数据类型参数传递方式

    测试证明,java基础数据类型参数传递值虽是引用传递但是值不会改变.对象是引用传递,值会改变. 为什么?找到一段话来解释这个问题. "对于字符串对象来说,虽然在参数传递的时候也是引用传递,但 ...

  2. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

  3. ORCAD中的一些操作小技巧

    1.ORCAD中改变元器件和文本字体颜色的命令: 打开在 View -> Toolbar -> Command Window.然后圈选文字(可复选),然后到 Command Window ...

  4. 30:根据排序标识flag给数组排序

    题目描述:输入整型数组和排序标识,对其元素按照升序或降序进行排序 接口说明 原型: void sortIntegerArray(Integer[] pIntegerArray, int iSortFl ...

  5. 【强网杯2018】逆向hide

    这是事后才做出来的,网上没有找到现成的writeup,所以在这里记录一下 UPX加壳,而且linux下upx -d无法解,也无法gdb/ida attach 因为是64位,所以没有pushad,只能挨 ...

  6. matlab2017b linux版分享

    链接:https://pan.baidu.com/s/1smrTkFN 密码:cvb3 下载后请点关注并点赞,谢谢支持.

  7. FTP匿名登录或弱口令漏洞及服务加固

    漏洞描述 FTP 弱口令或匿名登录漏洞,一般指使用 FTP 的用户启用了匿名登录功能,或系统口令的长度太短.复杂度不够.仅包含数字.或仅包含字母等,容易被黑客攻击,发生恶意文件上传或更严重的入侵行为. ...

  8. 触发器 (Delete Update)

    --delete触发器IF(EXISTS(SELECT * FROM sysobjects WHERE name='T_PlanQtyDelete'))DROP TRIGGER T_PlanQtyDe ...

  9. vim与sublime

    vim与sublime 对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持 ...

  10. spring boot json 首字母大小写问题解决方案

     spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个Js ...