题目链接

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

因为除了根节点, 一个节点上面只能有一个蚂蚁, 所以我们将根节点去掉, 于是就有了一个森林。 时间就是所有子树里面花费时间最多的那棵树的时间。

对于每棵子树, 我们将所有的节点标一个深度, 然后将深度排序。 可以知道 dp[i] = max(dp[i-1]+1, d[i]), d是深度, 这样就可以求得最终答案。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 5e5+;
int head[maxn*], num, cnt, dp[maxn], d[maxn];
struct node
{
int to, nextt, w;
}e[maxn*];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = ;
mem1(head);
}
void dfs(int u, int fa, int depth) {
int flag = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
flag = ;
dfs(v, u, depth+);
}
if(!flag) {
d[cnt++] = depth;
}
}
int main()
{
init();
int n, u, v, ans = ;
cin>>n;
for(int i = ; i<n; i++) {
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
for(int i = head[]; ~i; i = e[i].nextt) {
int v = e[i].to;
cnt = ;
dfs(v, , );
sort(d, d+cnt);
for(int j = ; j<cnt; j++) {
d[j] = max(d[j-]+, d[j]);
}
ans = max(ans, d[cnt-]);
}
cout<<ans<<endl;
return ;
}

codeforces 622E. Ants in Leaves的更多相关文章

  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. codeforces622E Ants in Leaves (dfs)

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

  5. [Educational Round 10][Codeforces 652F. Ants on a Circle]

    题目连接:652F - Ants on a Circle 题目大意:\(n\)个蚂蚁在一个大小为\(m\)的圆上,每个蚂蚁有他的初始位置及初始面向,每个单位时间蚂蚁会朝着当前面向移动一个单位长度,在遇 ...

  6. Codeforces 652F Ants on a Circle

    Ants on a Circle 感觉这个思路好巧妙啊. 我们能发现不管怎么碰撞,初始态和最终态蚂蚁间的相对顺序都是一样的, 并且所占的格子也是一样的, 那么我们就只需要 找到其中一个蚂蚁的最终位置就 ...

  7. CodeForces 318D Ants

    题目链接 题意: 有n仅仅蚂蚁和m次询问 n仅仅蚂蚁初始所有位于起点(0,0)处.每4仅仅蚂蚁在同一格就会以该格为中心向上下左右四个方向爬一格 一仅仅向上,一仅仅向下,一仅仅向左.一仅仅向右 假设每一 ...

  8. Educational Codeforces Round 7

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

  9. 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++ ...

随机推荐

  1. javascript原生ajax;

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 给Object扩展新方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 安卓activity捕获返回button关闭应用的方法

    安卓activity捕获返回button关闭应用的方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //按下键盘 ...

  4. C#深复制和浅复制

    本文在于巩固基础 我们来看看什么是深复制.浅复制: C#中对于数据的复制机制虽然简单但是容易让人误解.C#数据类型大体分为值类型(value type)与引用类型(reference type).对于 ...

  5. 算法精解(C语言描述) 第4章 读书笔记

    第4章 算法分析 1.最坏情况分析 评判算法性能的三种情况:最佳情况.平均情况.最坏情况. 为何要做最坏情况分析: 2.O表示法 需关注当算法处理的数据量变得无穷大时,算法性能将趋近一个什么样的值.一 ...

  6. 转 --maven系列之一 简介

    http://blog.csdn.net/jiuqiyuliang/article/details/41076215 [项目管理和构建]——Maven简介(一) 2015-01-31 21:27 68 ...

  7. 浅谈Mybatis(三)

    一.动态SQL 1.sql片段 解决sql语句的冗余代码问题. <sql id="SELECT_T_USER"> select id,name,password,bir ...

  8. 计算Date间的分钟数

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); Date date_begin = simpl ...

  9. hdu 4454 Stealing a Cake (三分)

    Stealing a Cake Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. js中递归解析xml

    xml结构: <RightMenuItems>  <Item Code="New" Name="新建" GroupCode="Edi ...