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

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

题意:给你一棵节点数为n的树,每一个叶子节点上有一只蚂蚁,每一秒树上的蚂蚁都可以走到父亲节点的位置,但是每一个节点(除根节点)最多只能有一只蚂蚁,问最少需要花费的时间。

思路:一定是先让深度小的蚂蚁走到根节点,因为如果有深度大的比深度低的先走到根节点,那么深度低的可能要先等深度大的,而自己没有走,但如果深度低的先走的话,那么深度大的和小的可以一起往根节点走,这样可以在相同的时间走更多的总步数。所以我们算出根节点下的每一个子树走完所要花的最大时间,然后更新答案就行。那么每一棵子树所要花的时间为t[i]=max(t[i-1]+1,deep[t]),即前面一个走到根节点的时间+1与其深度的较大值。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 500050
vector<int>pos[maxn];
vector<int>::iterator it;
int a[maxn],tot,vis[maxn];
void dfs(int u,int father,int deep)
{
int i,j;
vis[u]=1;
if(pos[u].size()==1){
tot++;a[tot]=deep;
return;
}
for(i=0;i<pos[u].size();i++){
if(father!=pos[u][i] ){
dfs(pos[u][i],u,deep+1 );
}
}
}
int main()
{
int n,m,i,j,ans,c,d,len;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)pos[i].clear();
for(i=1;i<=n-1;i++){
scanf("%d%d",&c,&d);
pos[c].push_back(d);
pos[d].push_back(c);
}
len=pos[1].size();
ans=0;
for(i=0;i<len;i++){
tot=0;
dfs(pos[1][i],1,1);
sort(a+1,a+1+tot);
a[0]=0;
for(j=1;j<=tot;j++){
a[j]=max(a[j-1]+1,a[j]);
}
ans=max(ans,a[tot]);
}
printf("%d\n",ans);
}
return 0;
}

codeforces622E Ants in Leaves (dfs)的更多相关文章

  1. codeforces 622E E. Ants in Leaves(贪心+dfs)

    题目链接: E. Ants in Leaves time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. 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 ...

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

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

  4. uva 699 The Falling Leaves dfs实现

    额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...

  5. codeforces 622E. Ants in Leaves

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

  6. [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  7. Educational Codeforces Round 7

    622A - Infinite Sequence    20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...

  8. HZNU 2019 Summer training 6 -CodeForces - 622

    A - Infinite Sequence  CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++ ...

  9. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

随机推荐

  1. 【Java基础】集合

    集合 集合概述 一方面, 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象 的操作,就要对对象进行存储.另一方面,使用 Array 存储对象方面具有一些弊端,而 Java 集合就像一种容器 ...

  2. 【Java基础】网络编程

    网络编程 网络编程概述 网络编程的目的:直接或简洁地通过网络协议与其他计算机实现数据交换,进行通讯. 网络编程的两个主要问题: 如果准确地定位网络上一台或多台主机,并定位主机上的特定应用: 找到主机后 ...

  3. Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)

    概述 在Excel中,应用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种条件格式.本篇文章,将通过Java程序示例介绍条件格式的设置方法,设置条 ...

  4. LAN-SHARE 使用教程

    Description: 我的个人项目 LAN-Share 的使用教程:局域网内文件分享工具 LAN-Share 是一个基于 Node.js 的用于局域网内文件分享的工具,易于配置与部署. 项目地址: ...

  5. 70.LeetCode爬楼梯

    爬楼梯 点击标题可跳转到官网进行查看 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: ...

  6. 【Linux】CentOS7中修改中文字符集

    CentOS 7中字符集查看的方式是 locale -a   或者locale 如果想显示中文的话,应该修改为 LANG="zh_CN.UTF-8" 在命令行界面临时修改字符集的话 ...

  7. 面试时通过volatile关键字,全面展示线程内存模型的能力

    面试时,面试官经常会通过volatile关键字来考核候选人在多线程方面的能力,一旦被问题此类问题,大家可以通过如下的步骤全面这方面的能力.     1 首先通过内存模型说明volatile关键字的作用 ...

  8. 1V升3V芯片,1V升3.3V芯片,大电流的,低功耗

    一般来说,1V的电压实在很低了,即使是干电池的话,再1V时,也是基本属于没电状态了.还有一种是干电池输出电流大时,也会把干电池的电压从1.5V拉低到1V左右. 更多的是客户对于1V时要能升到3V或者3 ...

  9. 记一次ceph pg unfound处理过程

    今天检查ceph集群,发现有pg丢失,于是就有了本文~~~ 1.查看集群状态 [root@k8snode001 ~]# ceph health detail HEALTH_ERR 1/973013 o ...

  10. centos 7.0 ping百度提示:ping: www.baidu.com: Name or service not known

    解决方法一: 添加dns服务器 vi /etc/resolv.conf 在文件中添加如下两行: nameserver 8.8.8.8 nameserver 8.8.4.4 保存退出,重启服务器.之后再 ...