dp优化专辑 E - Computer [树形dp]

此题郁闷死我了。。。今早一去厕所涣然大悟,一下子就明白了偷笑  回来一改就AC了微笑  那个激动哇~抓狂


题意:

求树中每个点(电脑)到所有叶子节点(电脑)的距离的最大值是多少


分析:

每个节点记录最大值和次大值,同时记录最大值来源于哪个结点,后面更新需要用到,第一次DFS计算最大值和次大值(从下往上),第二次更新最大值和次大值(从上往下),

更新最大值有两种情况

1、如果父节点的最大值来自该子节点,则该子节点要与父结点的次大值+权值进行比较(要是相等,令最大值来自于结点0,表示两条路都可以),同时更新该结点的次大值

2、如果父节点的最大值不来自该子节点,则该子节点要与父结点的最大值+权值进行比较

以上两种情况需要更新最大值来自于哪个结点


//AC CODE:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<map>
using namespace std;
#define Max(a,b) a>b?a:b
#define N 10005
int n;
struct node
{
    int father;
    int child;
    int brother;
    int from;//和父节点的权值
    int Right;//最大值
    int Left;//次大值
    int from_node;
    void init()
    {
        father=0;
        child=0;
        from=0;
        Left=0;
        Right=0;
        from_node=0;
    }
} p[N];
void dfs(int start)
{
    int child=p[start].child;
    while(child)
    {
        dfs(child);
        if(p[start].Right<=p[child].Right+p[child].from)
        {
            p[start].Left=p[start].Right;
            p[start].Right=p[child].Right+p[child].from;
            p[start].from_node=child;
        }
        else if(p[start].Left<p[child].Right+p[child].from)
            if(p[start].Left<p[child].Right+p[child].from)
                p[start].Left=p[child].Right+p[child].from;
        child=p[child].brother;
    }
//    for(int i=1; i<=n; i++)
//        printf("(%d %d) ",p[i].Right,p[i].Left);
//    printf("\n");
}
void next_dfs(int start)
{
    int child=p[start].child;
    while(child)
    {
        if(p[p[child].father].from_node==child)
        {
            if(p[child].Right==p[p[child].father].Left+p[child].from)
                p[child].from_node=0;//两条边可以达到最大值
            if(p[child].Right<p[p[child].father].Left+p[child].from)
            {
                p[child].Right=p[p[child].father].Left+p[child].from;
                p[child].from_node=p[child].father;
            }
            if(p[child].Left<p[p[child].father].Left+p[child].from)
                p[child].Left=p[p[child].father].Left+p[child].from;
        }
        else
        {
            if(p[child].Right==p[p[child].father].Right+p[child].from)
                p[child].from_node=0;
            if(p[child].Right<p[p[child].father].Right+p[child].from)
            {
                p[child].Right=p[p[child].father].Right+p[child].from;
                p[child].from_node=p[child].father;
            }
        }
        next_dfs(child);
        child=p[child].brother;
    }
}
int main(void)
{
    //freopen("in.txt","r",stdin);
    int a,b;
    while(scanf("%d",&n)!=EOF)
    {
        p[1].init();
        for(int i=2; i<=n; i++)
        {
            p[i].init();
            scanf("%d %d",&a,&b);
            p[i].father=a;
            p[i].brother=p[a].child;
            p[i].from=b;
            p[a].child=i;
        }
        dfs(1);
        next_dfs(1);
        for(int i=1; i<=n; i++)
            printf("%d\n",p[i].Right);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值