题目链接: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的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 26

    Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...

  3. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  4. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  5. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  6. [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 ...

  7. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  8. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  9. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

随机推荐

  1. UVa 1152 4 Values whose Sum is 0

    题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种 看的紫书,先试着用hash写了一下, 是用hash[]记录下来a ...

  2. [POJ 3498] March of the Penguins

    March of the Penguins Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4378   Accepted:  ...

  3. Js 读写cookies

    //写cookies函数 function setCookie(name, value)//两个参数,一个是cookie的名子,一个是值 { var Days = 30; //此 cookie 将被保 ...

  4. linq xml读取

    <?xml version="1.0" encoding="UTF-8" ?> <cache> <chatOld> < ...

  5. java分层架构概念

    转自:http://www.cnblogs.com/bdqnbenet/p/4924778.html service是业务层 DAO (Data Access Object) 数据访问 1.JAVA中 ...

  6. Java知识点:琐碎知识点(1)

    Java基本介绍 SUN:Stanford University NetworkJava之父:James GoslingJava的跨平台性因为有Java虚拟机,运行class文件.Java吉祥物:Du ...

  7. cocos2d-x 小技巧

    1.字符串 与 数据结构互转 CCPoint: CCPointFromString(); {x, y} CCSize: CCSizeFromString(); {w, h} CCRect: CCSiz ...

  8. HDU 3844 Mining Your Own Business(割点,经典)

    题意: 给出一个连通图,要求将某些点涂黑,使得无论哪个点(包括相关的边)撤掉后能够成功使得剩下的所有点能够到达任意一个涂黑的点,颜料不多,涂黑的点越少越好,并输出要涂几个点和有多少种涂法. 思路: 要 ...

  9. python - 沙盒环境 - virtualenv - 简明使用录

    1. 不讲安装,没意思 2. 使用 virtualenv ENV # 建立环境,ENV你可以随便定,看起来像是 mkdir ENV cd ENV # 进目录呗 source bin/activate ...

  10. 【C#学习笔记】窗口隐藏、最小化、最大化、正常化

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...