程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 2196 computer 樹狀dp

hdu 2196 computer 樹狀dp

編輯:C++入門知識

hdu 2196 computer 樹狀dp


Computer

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3731 Accepted Submission(s): 1886


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.
\


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.

<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPGgzIGNsYXNzPQ=="panel_title" align="left">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 有一個計算機組成的無向樹狀網絡,要求求出每台計算機到其他計算機的最大距離。 原先有1號計算機,在此之後沒加如一台計算機都與原有的一台計算機連接,並有距離c。題中數據較大,所以不能對每個節點進行遞歸求深度。仔細想想,在以1號計算機為根節點的情況下,每個節點的最大距離來自其父節點或其子節點。 來自子節點的情況比較簡單,我們只要對其求深度就行maxn[t] = max(maxn[ti] + l[ti][fa])。 而父節點就要分情況,對於求的t節點,其父節點為fa節點,若fa節點的最大距離不是經過t節點來的,那麼t節點的最大距離就是max(maxn[t], maxn[fa] + l[t][fa]);如果是經過t節點來的,那依然可以從fa節點過來,不過路徑不是最大距離那條而是次大距離那條——second_maxn[fa], max(maxn[t], second_max[fa] + l[t][fa]);因此,在記錄最大距離的同時也要記入次大距離。
#include 
#include 
#include 
#include 
using namespace std;

const int INF = 999999999;

struct Node {
    int v;
    int c;
    Node(){}
    Node(int V, int C) {
        v = V;
        c = C;
    }
};

int n;
std::vector v[10010];
int Fmax[10010];//first maxn
int Smax[10010];//second maxn
int visf[10010];//標記最大距離來自的電腦標號
int viss[10010];//標記次大距離來自的電腦標號

void init(int n) {
    for (int i=0; i<=n; i++) {
        v[i].clear();
    }
    for (int i=0; i<=n; i++) {
        Fmax[i] = Smax[i] = -INF;
    }
}

void dfs0(int t, int fa) {
    Fmax[t] = 0;
    Smax[t] = 0;
    for (int i=0; i> n) {
        init(n );
        int a, b;
        for (int i=2; i<=n; i++) {
            cin >> a >> b;
            v[i].push_back(Node(a,b));
            v[a].push_back(Node(i,b));
        }
        dfs0(1, -1);
        dfs1(1, -1, 0);
        for (int i=1; i<=n; i++) {
            cout << Fmax[i] <


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved