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\),问最多能抽出多少个数, ...
随机推荐
- JAVA中获取项目文件路径
在java中获得文件的路径在我们做上传文件操作时是不可避免的. web 上运行 1:this.getClass().getClassLoader().getResource("/" ...
- JAVA使用apache http组件发送POST请求
在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...
- bzoj3571
同样的最小乘积XXX模型,这里显然是二分图带权匹配 我不会写KM……于是写了个费用流,由于是稠密图,会退化到n^4 然后本地跑了56s,交上去过了………………一定是我电脑太慢…… 改天写个KM吧 *; ...
- rsync不存在用户处理CPU消耗拒绝服务漏洞
受影响产品: rsync 3.1.0 漏洞描述: CVE ID:CVE-2014-2855 rsync是一款文件同步管理软件. rsync处理不存在用户时存在安全漏洞,可消耗大量CPU资源,造成拒绝服 ...
- System.web.optimization 在 Asp.Net WebForm 中应用得注意了
我们也可以在Asp.Net WebForm项目中去使用Optimization,去处理我们的资源文件,从而起到优化网站性能的效果,前端知识得从小事做起.但是在使用过程中我却发现了下面的问题. 第一步: ...
- How to modify squashfs image
/********************************************************************** * How to modify squashfs ima ...
- I.MX6 Ethernet UI patch failed
/*********************************************************************** * I.MX6 Ethernet UI patch f ...
- 修改placeholder属性
input::-webkit-input-placeholder{ font-size:12px;}input:-ms-input-placeholder{ font-size:12px;}input ...
- [转] arcgis Engine创建shp图层
小生 原文 arcgis Engine创建shp图层 以创建点图层为例.首先要得到保存文件的地址. SaveFileDialog saveFileDialog = new SaveFileDialog ...
- 基于Fragment实现Tab的切换,滑出侧边栏
最近在学习Fragment(碎片)这是android3.0以后提出的概念,很多pad上面的设置部分都是通过Fragment来实现的,先看看具体的效果吧(图一) (图二) (图三)第一章图片是初始时的 ...