CodeForces 622E Ants in Leaves (贪心策略 递推公式)
来源:互联网 发布:淘宝详情页背景素材 编辑:程序博客网 时间:2024/04/28 00:03
Ants in Leaves
Description
Tree 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
121 21 31 42 52 63 73 83 98 108 118 12
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 1000005int 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;}
- CodeForces 622E Ants in Leaves (贪心策略 递推公式)
- codeforces 622E Ants in Leaves 数 贪心
- Educational Codeforces Round 7 E. Ants in Leaves(贪心)
- CodeForces 622 E.Ants in Leaves(dfs)
- Codeforces 622E Ants in Leaves【树型Dp】
- E. Ants in Leaves
- Educational Codeforces Round 7 E. Ants in Leaves(DFS+贪心)
- cf#ecr7- E - Ants in Leaves-暴力+贪心
- Codeforces edu 7 E. Ants in Leaves 图论 搜索
- Educational Codeforces Round 7-E. Ants in Leaves
- codeforces_622E. Ants in Leaves(dfs+排序+贪心)
- codeforcesE. Ants in Leaves
- codeforces622E Ants in Leaves (dfs)
- codeforces 337C Quiz(快速幂+推公式+贪心)
- Mishap in Club--codeForces 245E--贪心
- codeforces 732E (贪心)
- Codeforces 777E 贪心
- codeforces 777E 贪心
- JavaEE和JSP有什么区别
- Android之监测手机网络状态的广播
- ANSI库续(时间函数)与文件操作初识
- 第17章 二叉树,堆和优先队列(1/3)
- lower_bound()函数和upper_bound()函数
- CodeForces 622E Ants in Leaves (贪心策略 递推公式)
- 表达式计算器(逆波兰法)栈操作(C语言实现)
- 计算几何---判断点是否在线段上
- PyQt QString转成python stirng
- Linux的mmap内存映射机制解析
- Object-C中的内存管理 MRC、ARC以及Autoreleasepool的区别
- C++实现——排列组合
- 无序数组中最小的k个数
- ReactNative学习十五-横竖布局及右上角圆点