Educational Codeforces Round 7 - E. Ants in Leaves
题目链接:http://www.codeforces.com/contest/622/problem/E
题意是给你一棵树,1为根,每个叶子节点有一个蚂蚁,移动到一个邻接节点时间耗费为1,一个节点上不能同时有1个以上的蚂蚁数目(除了根节点外),让你求最后一个蚂蚁到达根节点的最少的时间。
可以先dfs预处理每个叶子节点的深度dep[i],然后把1节点邻接的节点当作一个子根,求这个子根所在的子树上蚂蚁到这个子根的最大的时间,除了子树上蚂蚁最早到达的时间为ans[0] ,其余子树上的蚂蚁为max(ans[i - 1] , dep[i] + 1),最后的答案就是遍历1所相邻的子根所得到的答案取一个最大值。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + ;
bool ok[MAXN];
vector <int> G[MAXN] , ans; void dfs(int u , int par , int dep) {
if(G[u].size() == ) {
ans.push_back(dep + );
return ;
}
for(int i = ; i < G[u].size() ; i++) {
if(G[u][i] != par) {
dfs(G[u][i] , u , dep + );
}
}
} int main()
{
int n , v , u;
scanf("%d" , &n);
for(int i = ; i < n ; i++) {
scanf("%d %d" , &u , &v);
G[u].push_back(v);
G[v].push_back(u);
}
int len = G[].size() , res = , temp;
for(int i = ; i < len ; i++) {
dfs(G[][i] , , );
sort(ans.begin() , ans.end());
temp = ans[];
for(int j = ; j < ans.size() ; j++) {
temp = max(temp + , ans[j]);
}
res = max(res , temp);
ans.clear();
}
printf("%d\n" , res);
}
Educational Codeforces Round 7 - E. Ants in Leaves的更多相关文章
- 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 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
随机推荐
- 对List顺序,逆序,随机排列实例代码
ackage Test; import java.util.Collections; import java.util.LinkedList; import java.util.List; p ...
- "=="和equals方法的区别
.==和equal .栈内存和对内存 单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量 ...
- SQL Server索引怎么用
什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...
- Raphael 目标点沿路径不断移动
<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8" ...
- HDU 4609 3-idiots (FFT-快速傅立叶变换)
[题意]给定N个树枝,求从中取出三个可以围成三角形的概率 [思路] 2013多校训练第一场比赛1010题. 一开始就想到了O(n^2)枚举前两个树枝和的算法,赛后群里大牛说计算所有两个树枝和的情况可以 ...
- 数据库语言(一):SQL语法实例整理
数据库系统以一些语句作为输入,并返回一些输出,例如sql查询总是返回一张表,我们定义:具有相同格式的记录的集合是一张表. 考虑大学数据库系统: SQL中的数据类型: char(n) 字符串长度为n ...
- 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载
原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/15334159 cocos2d-x中和Android,Windows都 一样, ...
- HDU 5119 Happy Matt Friends
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...
- Oracle数据库表结构导出
1. 在PL/SQL中找到"工具--导出用户对象"菜单.点击运行.
- SQL Server 高性能写入的一些总结(转)
1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...