codeforces 622E. Ants in Leaves
给一棵有根树, 每个叶子节点上有一只蚂蚁。 在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的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 7 - E. Ants in Leaves
题目链接:http://www.codeforces.com/contest/622/problem/E 题意是给你一棵树,1为根,每个叶子节点有一个蚂蚁,移动到一个邻接节点时间耗费为1,一个节点上不 ...
- codeforces622E Ants in Leaves (dfs)
Description Tree is a connected graph without cycles. A leaf of a tree is any vertex connected with ...
- [Educational Round 10][Codeforces 652F. Ants on a Circle]
题目连接:652F - Ants on a Circle 题目大意:\(n\)个蚂蚁在一个大小为\(m\)的圆上,每个蚂蚁有他的初始位置及初始面向,每个单位时间蚂蚁会朝着当前面向移动一个单位长度,在遇 ...
- Codeforces 652F Ants on a Circle
Ants on a Circle 感觉这个思路好巧妙啊. 我们能发现不管怎么碰撞,初始态和最终态蚂蚁间的相对顺序都是一样的, 并且所占的格子也是一样的, 那么我们就只需要 找到其中一个蚂蚁的最终位置就 ...
- CodeForces 318D Ants
题目链接 题意: 有n仅仅蚂蚁和m次询问 n仅仅蚂蚁初始所有位于起点(0,0)处.每4仅仅蚂蚁在同一格就会以该格为中心向上下左右四个方向爬一格 一仅仅向上,一仅仅向下,一仅仅向左.一仅仅向右 假设每一 ...
- Educational Codeforces Round 7
622A - Infinite Sequence 20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...
- 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++ ...
随机推荐
- jQuery $.each用法比较详细了
以下内容非原创,来自百度文库http://wenku.baidu.com/view/4796b6145f0e7cd18425368e.html 通过它,你可以遍历对象.数组的属性值并进行处理. 使用说 ...
- li span兼容性问题
li与span的搭配使用所产的浏览器兼容性问题 1.ls两位,设定了width还是没用.2.总结了一下就是,里面的标签漂浮以后,就不能撑起外层的容器了. 3.li要设至少一个宽度或高度,还要加上ove ...
- 2014.9.3数据库CRUD
CRUD 增删改查 DCL 数据控制语言:备份,grant DML 数据操作语言: CRUD DDL 数据定义语言:create drop alter 自增长列不能赋值 增: Insert into ...
- Java 字符串截取函数 substring()
在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...
- Java中单例七种写法(懒汉、恶汉、静态内部类、双重检验锁、枚举)
/*** * 懒汉模式 1 * 可以延迟加载,但线程不安全. * @author admin * */ public class TestSinleton1 { private static Test ...
- php-Eclipse对php中的namespace关键字报语法错误的问题
namespace是php5.3以上才支持的, 解决办法:升级IDE 切换Eclipse中的php版本 Window > Preferences > PHP > PHP Int ...
- 中国 省会 地级市 经纬度 city array
<?php $city_arr = array ( '北京' => array ( 'gis_lng' => '116.405285', 'gis_lat' => '39.90 ...
- 用python 10min手写一个简易的实时内存监控系统
简易的内存监控系统 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 文章github源地址,还可以看到具体的代码,喜欢请在原链接右上角加个star 腾讯视频链接 ...
- issues about Facebook Login
在学习The complete iOS 9 Developer Course - Build 18 Apps 中的Letture134-Facebook Login,需要整合(integrate)Pa ...
- C++可变参数的另一种实现
大家熟知的C库函数printf函数就是一个可变参数函数,它是怎么实现的呢?不过他实现是有条件的,必须函数参数的入栈顺序为从右向左的顺序,也即函数的形参,在函数调用之前,必须是最右边的参数先入栈,并且参 ...