Computer (树形dp 经典题)

题目:Computer

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.



Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
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

题意:刚开始有一台电脑,后来又买了n-1台电脑,给出连接状态,即给出了一颗树,求每个电脑能到达的最远距离是多少,即求树上的每个点能到达的最远的距离是多少。

输入:

第一个数n,表示有n个节点,后面n-1行,每行输入的是第i台电脑和第几台电脑相连,边权是多少,即第二行1 1表示第二台电脑和第一台电脑相连,距离是1。

思路:

这个题刚上来是没有思路的……甚至连题都看不懂。这个也是看的时间最长的一个题吧。

用struct建树,struct Tree(node)表示树上的每一个点。

这个题需要两个搜索。因为对于一个节点来说,它的最大距离可能来自于它的子树,另一个是来自于它的父节点。所以需要两个搜索。

第一次搜索求出任一节点在它的子树范围内到达叶子节点的最大距离和次大距离。为什么要求次大距离呢?因为如果只存最大值的话,判断其从父节点过来的最大值时,如果其父节点存的最大值刚好是从该点过来的,那么就失去了父节点过来的状态。第二次搜索就是从父节点过来的最大值。

head数组表示头结点,建树时需要。

源代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <cmath>
using namespace std;
struct Tree{
    int from,next,length;
}tree[100005];
int head[1000005];//头节点
int max_up_to_down[100005];//从该节点往下搜索得到的最大距离
int max_up_to_down_2[1000005];//从该节点往下搜索的次大距离
int maxid[1000005];//最大距离对应的序号
int maxid2[1000005];//次大距离对应的的序号
int num;
void build(int i,int con,int length){//构建树
    tree[num].from = con;
    tree[num].length = length;
    tree[num].next = head[i];
    head[i] = num++;
    tree[num].from = i;
    tree[num].length = length;
    tree[num].next = head[con];
    head[con] = num++;
}
//求节点v向下到叶子节点的最大距离
void tree_dp1(int v,int father){
    max_up_to_down[v] = 0;
    max_up_to_down_2[v] = 0;
    for (int i = head[v]; i != -1; i = tree[i].next){
        int v1 = tree[i].from;
        if (v1 == father)
            continue;
        tree_dp1(v1,v);
        if (max_up_to_down_2[v] < max_up_to_down[v1] + tree[i].length){
            max_up_to_down_2[v] = max_up_to_down[v1] + tree[i].length;
            maxid2[v] = v1;
            if (max_up_to_down_2[v] > max_up_to_down[v]){
                swap(max_up_to_down[v],max_up_to_down_2[v]);
                swap(maxid[v],maxid2[v]);
            }
        }
    }
}
//求父节点过来的长度
void tree_dp2(int v,int father){
    for (int i = head[v]; i != -1; i = tree[i].next){
        int v1 = tree[i].from;
        if (v1 == father)
            continue;
        if (v1 == maxid[v]){
            if (max_up_to_down_2[v1] < tree[i].length + max_up_to_down_2[v]){
                max_up_to_down_2[v1] = tree[i].length + max_up_to_down_2[v];
                maxid2[v1] = v;
                if (max_up_to_down_2[v1] > max_up_to_down[v1]){
                    swap(max_up_to_down[v1],max_up_to_down_2[v1]);
                    swap(maxid2[v1],maxid[v1]);
                }
            }
        }
        else {
            if (max_up_to_down_2[v1] < tree[i].length + max_up_to_down[v]){
                max_up_to_down_2[v1] = tree[i].length + max_up_to_down[v];
                maxid2[v1] = v;
                if (max_up_to_down_2[v1] > max_up_to_down[v1]){
                    if (max_up_to_down_2[v1] > max_up_to_down[v1]){
                        swap(max_up_to_down[v1],max_up_to_down_2[v1]);
                        swap(maxid2[v1],maxid[v1]);
                    }
                }
            }
        }
        tree_dp2(v1,v);
    }
}
int main(){
    int n;
    while (scanf("%d",&n)!=EOF){
        num = 0;
        memset(head,-1,sizeof(head));
        int con,length;//和哪一台连接,以及长度。
        for (int i = 2; i <= n; i++){
            scanf("%d%d",&con,&length);
            build(i,con,length);
        }
        tree_dp1(1,-1);
        tree_dp2(1,-1);
        for (int i = 1; i <= n; i++)
            printf("%d\n",max_up_to_down[i]);
    }
    return 0;
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值