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的操作;
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- collection系列用法-defaultdict()
defaultdict() 定义以及作用 返回一个和dictionary类似的对象,和dict不同主要体现在2个方面: 可以指定key对应的value的类型. 不必为默认值担心,换句话说就是不必担心有 ...
- OkHttp–支持SPDY协议的高效HTTP库
Android为我们提供了两种HTTP交互的方式: HttpURLConnection 和 Apache HTTP Client,虽然两者都支持HTTPS,流的上传和下载,配置超时,IPv6和连接池, ...
- Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...
- 关于虚拟机装kali-linux的联网问题
我用的是VMware Workstation11,近期装了一个kali-linux,想玩一下password破解.没想到装上之后网络连接显示的是活跃的却无法上网,我试过桥接等其他方式去联网,却依然无法 ...
- 怎么增加照片的KB大小
之前都是要想办法压缩图片的大小 今天有人发来一张1.8MB的图片让我帮忙调到30MB左右 一下子放大这么多着实有点茫然 之后想到了一个办法 首先把图片占用体积变大,是不会增加清晰度的,而减小占用体积却 ...
- matlab改变GUI和figure左上角图标的方法,并生成exe文件
1. GUI左上角图标的更改,假设GUI的Tag为figure1,在其OpeningFcn里添加h = handles.figure1; %返回其句柄newIcon = javax.swing.Ima ...
- Javascript知识——事件
O(∩_∩)O~~又是新的一周开始了,今天还是在继续学习Javascript知识,今天主要讲了事件的知识.现在就总结下吧. 事件 事件一般是用于浏览器和用户操作进行交互.最早是 IE 和 Netsca ...
- 使用x manager 连接Linux桌面
/usr/bin/xterm -ls -display $DISPLAY 需要安装xterm 服务
- Hibernate工作流程
Hibernate创建步骤 (五大核心接口:Configuration/SessionFactory/Session/Transaction/Query) 1.新建工程,导入需要的jar包. 2.利用 ...