CodeForces 622E Ants in Leaves (贪心策略 递推公式)

16 篇文章 0 订阅
5 篇文章 0 订阅

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

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值