Codeforces 622E Ants in Leaves【树型Dp】

解决一个有趣的问题:如何让所有蚂蚁从树的不同叶子节点出发,用最短时间全部移动到根节点?考虑了蚂蚁不能在同一节点相遇的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Ants in Leaves
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 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.

Examples
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
Input
2
2 1
Output
1

题目大意:


给你一颗具有N个节点的树,规定1作为根。

现在每个叶子节点上都有一只蚂蚁,一秒可以移动到相邻的一个点上。问所有蚂蚁都移动到根最少需要几秒。

现在要求除了根节点以外,每个点都最多只能有一只蚂蚁在上边。


思路(思路源自:http://blog.youkuaiyun.com/kirito_acmer/article/details/50682852):


对于贪心方案来讲,我们肯定是想先将距离根节点近的蚂蚁先移动到根节点。

这样做能够避免更多堵塞的情况。

所以我们首先将根节点去掉,那么对应会留下若干子树,答案肯定就是子树中需要最长时间的那个树的时间。

我们拿出所有与1直接相连的点,然后预处理出其子树所有叶子节点的深度dist【i】预处理出来

那么对应深度最低的那个蚂蚁走了之后,同深度的蚂蚁要在到达目的地的时间的基础上+1.

那么就有:dist【i】=max(dist【i-1】+1,dp【i】);满足这个状态转移的情况要求dist【i】此时是有序的。

那么预处理出来dist【i】之后,我们将其按照从小到大排序即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int dist[6500000];
vector<int>mp[6500000];
int tot;
void Dfs(int u,int from,int depp)
{
    if(mp[u].size()==1)
    {
        dist[tot++]=depp;
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u,depp+1);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        int output=0;
        for(int i=0;i<mp[1].size();i++)
        {
            tot=0;
            int v=mp[1][i];
            Dfs(v,1,1);
            sort(dist,dist+tot);
            for(int j=1;j<tot;j++)
            {
                dist[j]=max(dist[j-1]+1,dist[j]);
            }
            output=max(dist[tot-1],output);
        }
        printf("%d\n",output);
    }
}








### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值