codeforces 622E E. Ants in Leaves(贪心+dfs)
2016-08-18 10:50
531 查看
题目链接:E. Ants in Leavestime limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputTree 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 vertex 1. 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 integers xi, 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.Examplesinput
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputTree 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 vertex 1. 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 integers xi, 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.Examplesinput
12 1 2 1 3 1 4 2 5 2 6 3 7 3 8 3 9 8 10 8 11 8 12output
6input
2 2 1output
1 题意: 除根节点外每个节点只能容纳一只蚂蚁,现在每个叶子节点都有一个蚂蚁,问这些蚂蚁都到达根节点的最短时间是多少; 思路: 贪心,深度小的蚂蚁一定要先走才能保证时间最短,所有按深度排序后每个叶子节点的蚂蚁到达根节点的时间就是上一个节点的最大时间+1与其深度的相比的较大值,即ans[i]=max(ans[i-1]+1,dep[i]); AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=5e5+10; const int maxn=(1<<20)+14; const double eps=1e-12; int n,cnt; vector<int>ve ; int dep ,num ; void dfs(int cur,int fa,int deep) { int len=ve[cur].size(); for(int i=0;i<len;i++) { int x=ve[cur][i]; if(x==fa)continue; dfs(x,cur,deep+1); } if(len==1)dep[++cnt]=deep; } int main() { int u,v; read(n); For(i,1,n-1) { read(u);read(v); ve[u].push_back(v); ve[v].push_back(u); } int len=ve[1].size(),ans=0; for(int i=0;i<len;i++) { cnt=0; dfs(ve[1][i],1,1); sort(dep+1,dep+cnt+1); for(int j=1;j<=cnt;j++) { num[j]=max(num[j-1]+1,dep[j]); } ans=max(ans,num[cnt]); } //for(int i=0;i<len;i++)ans=max(ans,); print(ans); return 0; }
相关文章推荐
- Verilog HDL那些事_建模篇笔记(实验九:VGA驱动)
- PHPNow升级PHP版本为5.3.5的方法
- 推送
- 迭代器模式
- spring文件头解析
- jquery 获取url及url参数的方法
- js form表单相关操作
- 进程地址空间分布
- oracle hint 和 并行
- DAY18_SSL/TLS连接与TCP/IP连接
- PostgreSQL存储过程(函数)
- Azure Redis Cache (4) 配置和管理Redis Cache
- 自定义刻度的SeekBar
- log4j文件日志的使用
- manifest.mf文件属性详解
- apache、nginx正向代理与反向代理的区别
- 收藏哎哎哎
- 基于jquery把表单转成成json对象
- jQuery中的DOM操作整理
- Drop Materialized View with Fails with ORA-4020 and getting ORA-955 while Recreating MVIEW