• 【洛谷P1131】时态同步【树形dp】


    题目大意:

    题目链接:https://www.luogu.org/problem/P1131
    给出一棵树以及其一个特殊点,可以选择一些边似的这条边的长度加1。问要使得从特殊点到达所有叶子结点的路径长度一样最少需要增加多少。


    思路:

    这道题准确来说应该不算dpdp
    把这个点看做整棵树的根,那么我们就需要让所有叶子到根的距离相同。
    假设点xx的子树全部满足到叶子的距离相同,那么我们需要维护使得所有叶子到xx的距离也相同,那么显然最优方案是把xx到他的子节点的距离增加。
    我们设maxnmaxn表示节点xx与其叶子节点的最远距离。那么我们就要把它与叶子的边的距离增加至maxnmaxn。那么我们发现一条边的权值使用过了后就不会再使用,所以我们把边(x,y)ysonx(x,y)|yin son_x的距离就设为节点yy至其叶子的距离。这样均摊就为O(n)O(n)
    还是比较简单的。代码也相对较短。


    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    
    const int N=500010;
    int n,root,tot,f[N],head[N];
    ll ans;
    
    struct edge
    {
    	int next,to;
    	ll dis;
    }e[N*2];
    
    void add(int from,int to,int dis)
    {
    	e[++tot].to=to;
    	e[tot].dis=dis;
    	e[tot].next=head[from];
    	head[from]=tot;
    }
    
    ll dfs(int x,int fa)
    {
    	ll maxn=0;
    	for (int i=head[x];~i;i=e[i].next)
    	{
    		int v=e[i].to;
    		if (v!=fa)
    		{
    			e[i].dis+=dfs(v,x);
    			maxn=max(maxn,(ll)e[i].dis);
    		}
    	}
    	for (int i=head[x];~i;i=e[i].next)
    		if (e[i].to!=fa) ans+=maxn-(ll)e[i].dis;
    	return maxn;
    }
    
    int main()
    {
    	memset(head,-1,sizeof(head));
    	scanf("%d%d",&n,&root);
    	for (int i=1,x,y,z;i<n;i++)
    	{
    		scanf("%d%d%d",&x,&y,&z);
    		add(x,y,z); add(y,x,z);
    	}
    	dfs(root,0);
    	printf("%lld",ans);
    	return 0;
    }
    
  • 相关阅读:
    react的路由以及传值方法
    三连击
    给网页添加鼠标样式
    单词统计(续)
    个人课程总结
    构建之法阅读笔记02
    构建之法阅读笔记01
    第十六周总结
    计算最长英语单词链
    第十五周总结
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/11998046.html
Copyright © 2020-2023  润新知