Computer HDU - 2196 (树形dp)

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

题目大意:

让你求每个节点到其他节点的最远距离。

解题思路:

用树形dp可以解决,不过貌似也有人用树的直径过了。。。

我们来讲讲如何dp,一个节点到其他节点的最长距离无非两种情况:

          1.从该节点向下到达某个叶子节点。

          2.从该节点向上到达某个节点然后在向下到达某个叶子节点。

我们只需要求出这两种情况,然后比较大小输出大的就行了。

思路有了,我们来想一想如何求这两种情况,用dp[u][0]代表从编号为u的节点向下到达某个叶子节点的最远距离,那么状态转移方程可表述为dp[u][0] = max(dp[vi][0] + wi)  (v代表u的儿子节点,w为二者之间所连边的权值),这就是第一种情况。要求第二种情况,我们得维护一个次远距离,dp[u][1]代表从编号为u的节点向下到达某个叶子节点的次远距离。这样的话,我们在求u节点的最远距离的时候就可以利用u的父亲节点维护的最远距离和次远距离来求解u节点的值,具体方法就是求出dp[i][0], dp[i][1]之后,在来一次dfs,采用从根向叶子节点的方式求出从每个节点向上所能到达的最远距离,记为dp[i][2]。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>

using namespace std;

typedef long long ll;
const int maxn = 10100;

int n, be, en;

ll dist[maxn], dp[maxn][4], son[maxn];
vector < ll > gra[maxn], val[maxn];

void dfs1(int u, int father)       //用来求dp[i][0], dp[i][1];
{
	ll ans = 0;
	int id = -1;
	for(int i = 0; i < gra[u].size(); ++ i)
	{
		int v = gra[u][i];
		ll w = val[u][i];
		if(v != father)
		{
			dfs1(v, u);
			if(ans < dp[v][0] + w)
			{
				ans = dp[v][0] + w;
				id = v;      //要保存下来,之后求dp[i][2]也要用;
			}
		}
	}
	for(int i = 0; i < gra[u].size(); ++ i)
	{
		int v = gra[u][i];
		ll w = val[u][i];
		if(v != father && v != id)
		{
			dp[u][1] = max(dp[u][1], dp[v][0] + w);
		}
	}
	dp[u][0] = ans;
	son[u] = id;
}

void dfs2(int u, int father)       //求dp[i][2]
{
	for(int i = 0; i < gra[u].size(); ++ i)
	{
		int v = gra[u][i];
		ll w = val[u][i];
		if(v != father)
		{
			if(son[u] == v)         //采用了从根向下更新的方式
			{
				dp[v][2] = max(dp[u][2], dp[u][1]) + w;   //如果v正好是最远距离的经过的节点就和次远距离dp[i][1]比较
			}
			else
			{
				dp[v][2] = max(dp[u][2], dp[u][0]) + w;   //否则就和最远距离dp[i][0]比较
			}
			dfs2(v, u);        //所以先求值在向下递归
		}
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	while(cin >> n)
	{
		ll v, w;
		memset(dp, 0, sizeof(dp));
		for(int i = 1; i <= n; ++ i)
			gra[i].clear(), val[i].clear();
		for(int i = 2; i <= n; ++ i)
		{
			scanf("%lld%lld", &v, &w);
			gra[i].push_back(v);
			gra[v].push_back(i);
			val[i].push_back(w);
			val[v].push_back(w);
		}
		dfs1(1, 0);
		dfs2(1, 0);
		for(int i = 1; i <= n; ++ i)
		{
			cout << max(dp[i][0], dp[i][2]) << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/85062039