hdu2196 树形dp

本文针对一个学校计算机网络的问题,通过两次深度优先搜索算法求解每个节点到其他节点的最大距离。首次搜索确定每个节点子树中最大长度及对应子树,第二次搜索则用于更新所有节点的最远距离。

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

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196

 

Problem Description
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.  
Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
 
Sample Output
3 2 3 4 4

总算彻底理解这个题了 =.=

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=10005;

struct Edge
{
    int v;
    int len;
    int next;
} edge[maxn<<1];

int head[maxn];
int fm[maxn],fn[maxn]; ///最远距离,以及对应的子树的序号
int sm[maxn],sn[maxn];  ///次远距离,以及其对应的子树
int k;

void addedge(int u,int v,int l)
{
    edge[k].v=v;
    edge[k].len=l;
    edge[k].next=head[u];
    head[u]=k++;

    edge[k].v=u;
    edge[k].len=l;
    edge[k].next=head[v];
    head[v]=k++;
}

void dfs1(int u,int p)      ///第一次dfs找到子树种最大的长度,和最大长度的子树的序号
{
    fm[u]=0;   ///初始化
    sm[u]=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==p) continue;
        dfs1(v,u);
        if(edge[i].len+fm[v]>sm[u])
        {
            sm[u]=edge[i].len+fm[v];
            sn[u]=v;
            if(sm[u]>fm[u])
            {
                swap(fm[u],sm[u]);
                swap(fn[u],sn[u]);
            }
        }
    }
}

void dfs2(int u,int p)
{
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==p) continue;
        if(v==fn[u])
        {
            if(edge[i].len+sm[u]>sm[v])
            {
                sm[v]=edge[i].len+sm[u];
                sn[v]=u;                   ///开始没懂为什么还要记录结点
                if(sm[v]>fm[v])            ///更新的目的就是可以找到从子树来的最大值,不记录有时候找的是次大值
                {
                    swap(fm[v],sm[v]);
                    swap(fn[v],sn[v]);
                }
            }
        }
        else
        {
            if(edge[i].len+fm[u]>sm[v])
            {
                sm[v]=edge[i].len+fm[u];
                sn[v]=u;
                if(sm[v]>fm[v])
                {
                    swap(fm[v],sm[v]);
                    swap(fn[v],sn[v]);
                }
            }
        }
        dfs2(v,u);
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        memset(head,-1,sizeof(head));
        k=0;
        int x,l;
        for(int i=2; i<=n; i++)
        {
            scanf("%d%d",&x,&l);
            addedge(i,x,l);
        }
        dfs1(1,-1);      ///随便选一点作为树根,那么他的父亲结点就设为-1
        dfs2(1,-1);       ///所以换成dfs1(2,-1),dfs2(2,-1)  也是可以的
        for(int i=1; i<=n; i++)
            printf("%d\n",fm[i]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/a-clown/p/6010109.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值