题目链接:

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. redis实现訪问频次限制的几种方式

    结合上一篇文章<redis在学生抢房应用中的实践小结>中提及的用redis实现DDOS设计时遇到的expire的坑.事实上,redis官网中对incr命令的介绍中已经有关于怎样用redis ...

  2. Xshell 一款很养眼的配色方案推荐

    Xshell 是个很好用的在 windows 下登陆 liunx 的终端原生支持中文,配合 Xftp 管理文件,同是免费软件可远比 Putty 好用多了面对枯燥的代码,我们需要一款很养眼的配色方案来保 ...

  3. 如何修改eclipse的默认字符集和修改中文乱码

    转载,以供以后学习.谢谢 有时候 java代码,导入eclipse中会出现 乱码的问题,通过修改字符集就可以解决. 看下面图片演示过程. 发表在 使用教程 | 标签为 eclipse, 乱码 | 留下 ...

  4. Html5上传插件封装

    前段时间将flash的上传控件替换成使用纯js实现的,在此记录 1.创建标签 <div class="camera-area" style="display:inl ...

  5. web安全系列(一):XSS 攻击基础及原理

    跨站脚本攻击(XSS)是客户端脚本安全的头号大敌.本文章深入探讨 XSS 攻击原理,下一章(XSS 攻击进阶)将深入讨论 XSS 进阶攻击方式. 本系列将持续更新. XSS 简介 XSS(Cross ...

  6. OpenGL研究3.0 多边形区域填充

    OpenGL研究3.0 多边形区域填充 DionysosLai(906391500@qq.com)2014-06-22 所谓多边形区域填充.就是将多边形内部区域,所有已相同色块填充.注意:这里讨论的多 ...

  7. Struts2学习八----------接收参数

    © 版权声明:本文为博主原创文章,转载请注明出处 接收参数 - 使用Action的属性接收参数 - 使用Domain Model接收参数 - 使用ModelDriven接收参数 实例 1.项目结构 2 ...

  8. uwsgi报错:listen queue of socket ...

    Linux默认的socket链接为128,uwsgi默人的链接为100 需要修改系统默认的配置参数, 然后修改uwsgi配置:listen参数:1024

  9. 优秀JS学习站点

    第一个:电子书类集合站点:http://www.javascriptcn.com/thread-2.html 第二类:移动端博客学习: https://segmentfault.com/a/11900 ...

  10. Spring里通过注解开启事物

    方式1 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://w ...