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++ ...
随机推荐
- android 回调
调函数(callback Function),顾名思义,用于回调的函数. 回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用 ...
- jvm的内存管理【转】
[转]JVM内存管理 这些日子一直在研究jvm内存管理的东西,网上的知识很多,总结一下,能沉淀下来的就是自己的! 首先,刚学java的时候就知道java类文件是以 .java为后缀的文件,经过java ...
- JavaSE思维导图(四)
- Kettle 学习笔记
一直用SSIS做ETL,越来越感觉这玩意不是亲生的.因此萌生换ETL工具的想法,不过Kettle社区版没什么调度系统,貌似错误处理也不是很方便,且先了解吧. 本文简略的记录了整个软件的使用流程. 开始 ...
- [转]oracle误删数据的恢复
与数据打交道,免不了会误删一些数据,之后还commit了,连回滚的机会都没了,而更糟糕的是你又没有备份,这种事终于在今天被我不幸的遇上了... 唯一一点值得欣慰的是,我删除表记录的时候,时间不长,一天 ...
- C# 创建文件时,文件夹不存在,如何自动创建文件夹
c# 创建文件时怎么创建文件夹?strhtml=......StreamWriter sw=new StreamWriter("D:/test/1.aspx",false);sw. ...
- pandas的札记
导入导出数据 在导入,导出DataFrame数据时,会用到各种格式,分为 to_csv ;to_excel;to_hdf;to_sql;to_json;to_msgpack ;to_html;to_g ...
- nyist 220 推桌子
题目链接:推桌子 题目意思:给你一些操作,将S出的桌子推到L出,但是这个过道有时会被占用,推一次是10min,不影响的操作可以同时开始,并且只记一次. 思路:贪心,首先按照S从小到大排序,决策:从第一 ...
- Linux的几个概念,常用命令学习
Linux的几个概念,常用命令学习---------------------------------设备名装载点// 通过装载点访问设备-------------------------------- ...
- 转 C语言面试题大汇总
转 C语言面试题大汇总,个人觉得还是比较全地!!! \主 题: C语言面试题大汇总,个人觉得还是比较全地!!! 作 者: free131 (白日?做梦!) 信 誉 值: 100 ...