HDU 2196 Computer 樹形dp

題目鏈接

Computer

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


 

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.

 

 

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

 

給你一顆樹,以及邊的邊權,求每個點最遠的點。

如果是求一個點的最遠的點,直接無向邊建樹dfs即可。但是本題這樣複雜度O(n^{2})肯定是不行的,只能轉換思路。

我們發現所謂最遠的點一定是這個點到那個點的一條鏈,我們稱之爲最遠鏈,那麼只有兩種可能,最遠鏈要麼在子樹,要麼經過父親節點。

在子樹的最遠鏈可以一遍dfs解決,但在dfs的時候,我們同時儲存dp[u][0]最遠鏈和dp[u][1]次遠鏈,一會有用。

我們知道根節點沒有父親節點,用dp[u][2]存儲走父親節點的最大值,dp[1][2]=0。

v是u的兒子節點,w[i]是節點i接近根節點的那條邊的邊權。

考慮其他節點經過父親節點的情況。那麼又要分兩種情況,

1、兒子在父親最遠鏈,dp[v][0]+w[v]==dp[u][0],此時兒子走父親有兩種情況,一種是走父親的次短鏈,另一種就是走“父親的走父親節點最遠鏈”,就是走爺爺節點。dp[v][2]=max(dp[u][1],dp[u][2])+w[v].

2、兒子不在父親的最遠鏈,dp[v][0]+w[v]!=dp[u][0],那麼兒子要麼走父親最遠鏈,要麼走爺爺節點。dp[v][2]=max(dp[u][0],dp[u][2])+w[v].

最後每個點的結果就是max(dp[u][0],dp[u][2])。

 

#include <iostream>
#include <algorithm>
#include <cstring>
#define ms(a,b) memset(a,b,sizeof(a))
#include <vector>
using namespace std;
const int N=1e4+10;
vector<int>tr[N];
int w[N],dp[N][3];
void dfs1(int u)//子樹,從下往上更新
{
	for(int i=0;i<tr[u].size();i++)
	{
		int v=tr[u][i];
		dfs1(v);
		if(dp[v][0]+w[v]>=dp[u][0])//更新子數最遠點和次遠點
		{
			dp[u][1]=dp[u][0];
			dp[u][0]=dp[v][0]+w[v];
		}
		else
		if(dp[v][0]+w[v]>dp[u][1])
			dp[u][1]=dp[v][0]+w[v];
	}
}
void dfs2(int u)//父親節點,從頂往下更新
{
	for(int i=0;i<tr[u].size();i++)
	{
		int v=tr[u][i];
		if(dp[v][0]+w[v]==dp[u][0])//dp[u][0]==dp[u][1]//v在u的最長鏈
			dp[v][2]=max(dp[u][1],dp[u][2])+w[v];
		else//v不在u的最長鏈
			dp[v][2]=max(dp[u][0],dp[u][2])+w[v];
		dfs2(v);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,x;
	while(cin>>n)
	{
		ms(dp,0);
		for(int i=1;i<=n;i++)	
			tr[i].clear();
		for(int i=2;i<=n;i++)
		{
			cin>>x>>w[i];
			tr[x].push_back(i);
		}
		dfs1(1);
		dp[1][2]=0;//初始化根節點,不存在走父親節點最遠鏈
		dfs2(1);
		for(int i=1;i<=n;i++)	
			cout<<max(dp[i][0],dp[i][2])<<endl; //子樹最遠和走父親節點最遠取max
	}
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章