前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 149 - Computer Network

ACMSGURU 149 - Computer Network

作者头像
Reck Zhang
发布2021-12-18 11:51:23
3080
发布2021-12-18 11:51:23
举报
文章被收录于专栏:Reck ZhangReck Zhang

Computer Network

Problem Description

A school bought the first computer some time ago. 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 for each computer number Si - maximum distance, 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

There is natural number N (N<=10000) in the first line of input, 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

Write N lines in output file. i-th line must contain number Si for i-th computer (1<=i<=N).

Example(s)

Input 3 1 1 1 2

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    uint64_t n;
    std::cin >> n;

    const uint64_t NETWORK_NODE_MAX = 1e5 + 7;
    // index, path
    std::vector<std::vector<std::pair<uint64_t , uint64_t >>> network(NETWORK_NODE_MAX, std::vector<std::pair<uint64_t , uint64_t >>{});
    std::vector<std::priority_queue<uint64_t , std::vector<uint64_t >, std::greater<uint64_t >> > result(NETWORK_NODE_MAX, std::priority_queue<uint64_t , std::vector<uint64_t >, std::greater<uint64_t >>{});
    std::vector<std::priority_queue<uint64_t , std::vector<uint64_t >, std::greater<uint64_t >>> dp(NETWORK_NODE_MAX, std::priority_queue<uint64_t , std::vector<uint64_t >, std::greater<uint64_t >>{});

    // max, second max
    auto get_heap_result = [](std::priority_queue<uint64_t , std::vector<uint64_t >, std::greater<uint64_t >>& heap) -> std::pair<uint64_t , uint64_t > {
        if(heap.empty()) {
            return {0, 0};
        }
        uint64_t second_max = heap.top();
        heap.pop();
        if(heap.empty()) {
            heap.push(second_max);
            return {second_max, 0};
        }
        uint64_t max = heap.top();
        heap.push(second_max);
        return {max, second_max};
    };

    for(uint64_t i = 2; i <= n; i++) {
        uint64_t father = 0;
        uint64_t path = 0;
        std::cin >> father >> path;
        network[father].push_back({i, path});
    }

    auto dfs = std::function<uint64_t (uint64_t )>{};
    dfs = [&](uint64_t index) -> uint64_t {
        for(const auto& next : network[index]) {
            dp[index].push(dfs(next.first) + next.second);
            if(dp[index].size() > 2) {
                dp[index].pop();
            }
        }
        return get_heap_result(dp[index]).first;
    };
    dfs(1);

    // index
    std::queue<uint64_t > bfs;
    bfs.push(1);
    result[1] = dp[1];
    while(!bfs.empty()) {
        uint64_t index = bfs.front();
        bfs.pop();
        for(const auto& next : network[index]) {
            uint64_t next_index = next.first;
            uint64_t path = next.second;
            uint64_t father_max_path = get_heap_result(result[index]).first;
            uint64_t father_second_path = get_heap_result(result[index]).second;
            uint64_t son_max_path = get_heap_result(dp[next.first]).first;
            uint64_t son_second_path = get_heap_result(dp[next.first]).second;
            result[next_index].push(son_max_path);
            result[next_index].push(son_second_path);
            result[next_index].push(father_max_path - son_max_path == path ?
                                        path + father_second_path
                                        :
                                        path + father_max_path);
            result[next_index].pop();
            bfs.push(next_index);
        }
    }
    for(uint64_t i = 1; i <= n; i++) {
        std::cout << get_heap_result(result[i]).first << std::endl;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-12-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Computer Network
    • Problem Description
      • Input
        • Output
          • Example(s)
            • Solution
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档