CodeForces 622E Ants in Leaves (贪心策略 递推公式)
2016-04-07 19:13
399 查看
Ants in Leaves
DescriptionTree is a connected graph without cycles. A leaf of a tree is any vertex connected with exactly one other vertex.
You are given a tree with n vertices and a root in the vertex1. There is an ant in each leaf of the tree. In one second some ants can simultaneously go to the parent vertex from the vertex
they were in. No two ants can be in the same vertex simultaneously except for the root of the tree.
Find the minimal time required for all ants to be in the root of the tree. Note that at start the ants are only in the leaves of the tree.
Input
The first line contains integer n (2 ≤ n ≤ 5·105) — the number of vertices in the tree.
Each of the next n - 1 lines contains two integersxi, yi (1 ≤ xi, yi ≤ n)
— the ends of the i-th edge. It is guaranteed that you are given the correct undirected tree.
Output
Print the only integer t — the minimal time required for all ants to be in the root of the tree.
Sample Input
Input
12 1 2 1 3 1 4 2 5 2 6 3 7 3 8 3 9 8 10 8 11 8 12
Output
6
题意:给出一颗根节点为1的树,树的每个叶子结点都有一只蚂蚁,现在所有蚂蚁要到根节点1去问最少时间多少, 每秒钟蚂蚁移动一个结点,每个结点除根节点外只能有一个蚂蚁。 分析: 看到是一棵树,很容易被误导去想树形dp,其实贪心可解 首先对于根节点的每一棵子树来说,如果想让时间最少,肯定要让深度小的叶子结点上的蚂蚁先移动, 这样就避免了后面蚂蚁的“堵塞”,基于这样一种考虑,我们把一棵子树上的所有叶子深度都取出来, 从小到大排序,对于相同深度的叶子结点,一定是前者已有的步数+1(因为同深度的叶子去同一个根 节点会发生堵塞),而深度不同的叶子,要么深度大于当前已经处理出的答案,那么一定不会与前面相堵塞, 更新为深度,如果小于则一定堵塞,答案+1。那么就有递推公式dp[i]=max(dp[i-1]+1,dp[i]); 注意题中给出的是无向边。#include<cstring> #include<string> #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<map> #include<cstdlib> #include<cmath> #include<vector> //#pragma comment(linker, "/STACK:1024000000,1024000000"); using namespace std; #define INF 0x3f3f3f3f #define maxn 1000005 int dp[maxn],vis[maxn],in[maxn]; int fir[maxn],nex[maxn],u[maxn],v[maxn]; int e_max; int num; void init() { memset(in,0,sizeof in); memset(vis,0,sizeof vis); memset(fir,-1,sizeof fir); e_max=0; } void add_edge(int s,int t) { int e=e_max++; u[e]=s; v[e]=t; nex[e]=fir[s]; fir[s]=e; } void dfs(int k,int deep) { vis[k]=1; if(in[k]==1) { dp[num++]=deep; return ; } for(int i=fir[k]; ~i; i=nex[i]) { if(!vis[v[i]]) dfs(v[i],deep+1); } } int main() { int n; while(scanf("%d",&n)!=EOF) { init(); for(int i=1; i<n; i++) { int a,b; scanf("%d%d",&a,&b); add_edge(a,b); add_edge(b,a); in[a]++; in[b]++; } int ans=0; dp[0]=0; vis[1]=1; for(int i=fir[1]; ~i; i=nex[i]) { num=1; dfs(v[i],1); sort(dp,dp+num); for(int j=1; j<num; j++) dp[j]=max(dp[j-1]+1,dp[j]); ans=max(dp[num-1],ans); } printf("%d\n",ans); } return 0; }
相关文章推荐
- 如何解决sublime text 2总是在新窗口中打开文件及文件夹
- 【算法】PHP实现经典算法(上)
- 网易笔试题 炮台攻击
- .ipp文件是做什么的
- Leetcode_231_Power of Two
- java this关键字的使用
- 使用 gitosis 创建git仓库
- 终于将tiny-sdpm+intellij idea+mysql+jettyt运行成功
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)
- [转]21分钟 MySQL 入门教程
- 类的继承
- lower_bound()函数和upper_bound()函数
- 剑指offer面试题 扑克牌的顺子
- asp.net TextBox文本框背景透明+浅色背景文字提示
- 顺序表
- CentOS中关于cannot find -lmysqlclient的解决
- Jquery动态删除添加表格中的内容
- 算法学习【6】寻找最小的k个,
- Spring中配置和读取多个Properties文件--转
- postgresql安装教程