首页 > 其他分享 >F. Minimum Maximum Distance

F. Minimum Maximum Distance

时间:2024-07-14 16:18:45浏览次数:8  
标签:Distance int max1 Maximum next max2 Minimum now dis

原题链接

题解

1.假设有一个以标记点 \(c\) 为根的子树,且子树内没有其他标记点,易得该子树内所有点的 \(f\leq f(c)\),所以我们可以把该子树内的非标记点全部删掉

2.完成步骤1之后,图就变成了所有叶子节点均为标记点的树

3.题目等价于求该树内,最小的点到边界的最大值,也就是求树的直径的一半

4.为什么?我们把树的直径高亮,对于任意点有 \(f\geq k+len/2\) (先到中点,再到两端)

实施

以标记点为根建树,直径一定经过某个点,所以树形 \(dp\),经过某个点的最长直径等于 第一大子树高度 + 第二大子树高度

code

#include<bits/stdc++.h>
#define ll long long
using namespace std;

vector<int> G[200005];
int mark[200005];
int dis[200005];
int vis[200005];

int len=0;

void dfs(int now,int fa)
{
    int max1=0,max2=0;
    if(mark[now]) vis[now]=1;
    for(auto next:G[now])
    {
        if(next==fa) continue;

        dfs(next,now);
        if(vis[next])
        {
            if(dis[next]+1>=max1)
            {
                max2=max1;
                max1=dis[next]+1;
            }
            else if(dis[next]+1>max2) max2=dis[next]+1;
            vis[now]=1;
        }
    }
    dis[now]=max1;
    len=max(len,max1+max2);
}

void solve()
{

    int n,k;
    cin>>n>>k;

    for(int i=1;i<=n;i++)
    {
        G[i].clear();
        vis[i]=0;
        mark[i]=0;
        dis[i]=0;
    }
    int start=0;
    for(int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        start=x;
        mark[x]=1;
    }

    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    dfs(start,start);
    cout<<(len-len/2)<<'\n';
    len=0;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--) solve();
    return 0;
}

标签:Distance,int,max1,Maximum,next,max2,Minimum,now,dis
From: https://www.cnblogs.com/pure4knowledge/p/18301696

相关文章

  • D. Distance in Tree
    原题链接题解固定一个点作为树的根,易得任意一条链,都可以以某个点作为最高点,且链的两端到最高点之和为\(k\)那么不难想到遍历每个点作为最高点那么接下来就变成了在子树里选两个端点,使得到该点的距离之和为\(k\)这种无序点对统计,我们可以顺序遍历,然后对于每一个遍历到的点,计......
  • Minimum_jerk参考代码
    1.参考代码importnumpyasnpimportmatplotlib.pyplotaspltfromcvxoptimportmatrix,solversdefgenQk(T_down,T_up):Q=np.zeros((6,6))Q[3][4]=72*(T_up**2-T_down**2)Q[3][5]=120*(T_up**3-T_down**3)Q[4][5]=360*(T_up*......
  • LeetCode 857. Minimum Cost to Hire K Workers
    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-hire-k-workers/description/题目:Thereare n workers.Youaregiventwointegerarrays quality and wage where quality[i] isthequalityofthe ith workerand wage[i] istheminimumwagee......
  • Leetcode 3203. Find Minimum Diameter After Merging Two Trees
    Leetcode3203.FindMinimumDiameterAfterMergingTwoTrees1.解题思路2.代码实现题目链接:3203.FindMinimumDiameterAfterMergingTwoTrees1.解题思路这一题的话算是一个拓扑树的题目?总之就是从树的叶子节点不断向上遍历,不断地删除已访问的叶子节点,并加入......
  • C - Tile Distance 2
    C-TileDistance2https://atcoder.jp/contests/abc359/tasks/abc359_c 思路在x方向上,让s<t然后如果s在tile的左边,移动到右边, 如果t在tile的右边,移动到左边,计算x和y方便的必走的steps,y方向上容易计算(跨的格子就是),x方向有些复杂,s在x方向上,不用花费(配合y方向上走步......
  • ABC359 G - Sum of Tree Distance
    题目链接题目大意给出一棵树,树上每个节点都有一种颜色,求所有颜色相同的节点两两之间距离的总和。 题解想来写题解主要是看了一下官方解法都写的需要“重心分解”,应该是对应中文语境下的树的点分治。实际上点分治写起来很费事,可以用启发式合并替代。具体来说,dfs时每个节点......
  • abc359_G Sum of Tree Distance 题解
    题目链接:Atcoder或者洛谷先考虑暴力,显然是枚举整棵树的路径,这个枚举复杂度显示是\(O(n^2)\),还不考虑计算\(f(i,j)\),考虑使用点分治优化枚举复杂度:\(O(n\log{n})\)。接下来考虑如何计算每条路径的\(f(i,j)\),注意到\(f(i,j)\):当且仅当\(a[i]=a[j]\)时,答案加上\(dis(i,j......
  • LeetCode 2268. Minimum Number of Keypresses
    原题链接在这里:https://leetcode.com/problems/minimum-number-of-keypresses/description/题目:Youhaveakeypadwith 9 buttons,numberedfrom 1 to 9,eachmappedtolowercaseEnglishletters.Youcanchoosewhichcharacterseachbuttonismatchedtoaslong......
  • LeetCode 2340. Minimum Adjacent Swaps to Make a Valid Array
    原题链接在这里:https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/题目:Youaregivena 0-indexed integerarray nums.Swaps of adjacent elementsareabletobeperformedon nums.A valid arraymeetsthefollowingco......
  • 209. Minimum Size Subarray Sum
    Givenanarrayofpositiveintegersnumsandapositiveintegertarget,returntheminimallengthofasubarraywhosesumisgreaterthanorequaltotarget.Ifthereisnosuchsubarray,return0instead.Example1:Input:target=7,nums=[2,3,1,2,......