week 6 A Krypton Belt East

topic:

There was a computer (number 1) in the laboratory. Recently, Kryptonian Kurudong purchased N-1 computers for the laboratory, numbered 2 to N. Each computer is connected to a previously installed computer with a network cable. But Gugudong is worried that the network speed is too slow, he wants to know the maximum cable length from the i-th computer to other computers, but poor Gugudong has just suffered a de-smart attack from cosmic rays not long ago.
Tip: The sample input corresponds to this figure. From this figure, you can see that the computer farthest from computer 1 is computer 4, and the distance between them is 3. Computer No. 4 and Computer No. 5 are the farthest points from Computer No. 2, so the answer is 2. Computer No. 5 is the farthest away from Computer No. 3, so for Computer No. 3, its answer is 3. Similarly, we can calculate that the answer for computer 4 and computer 5 is 4.

Input:

The input file contains multiple sets of test data. For each set of test data, the first line is an integer N (N <= 10000), then there are N-1 lines, two numbers in each line, and for the two numbers in the i-th line, they represent the number connected to computer i The computer number and the length of the network cable between them. The total length of the network cable will not exceed 10 ^ 9, and each number is separated by a space.

Output

For each set of test data, N lines are output, and the i-th line represents the answer of computer i (1 <= i <= N).

Sample Input

5
1 1
2 1
3 1
1 1

Sample Output

3
2
3
4
4

Ideas:

If the diameter of the tree is required, the problem only needs to go through dfs twice, but this problem is to find the longest chain length from each point of the tree, then the distance between any node and the two ends of the tree diameter , Ie dfs three times.
Pick a point 0, deep search from 0 to find the point x farthest from it, find the distance d1 [] from each point on the tree to x, then start deep search from x to find the point farthest from x, and then Assign to x, then find the distance d2 [] from each point on the tree to x, then the longest chain length starting from the i-th point is max (d1 [i], d2 [i]).

Code:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn = 10010;
int head[maxn], d1[maxn], d2[maxn], x, n, tot;
struct edge 
{ 
	int to, next, w;
}edges[2 * maxn];

void add(int u, int v, int w) 
{
	edges[tot] = { v, head[u], w };
	head[u] = tot;
	tot++;
}

void dfs(int u, int f, int* d) 
{
	for (int e = head[u]; e != -1; e = edges[e].next) 
	{
		int to = edges[e].to;
		if (to != f)
		{
			d[to] = d[u] + edges[e].w;
			if (d[x] < d[to])
				x = to;
			dfs(to, u, d);
		}
	}
}

int main() 
{
	int v, w;
	while (scanf_s("%d", &n)!=EOF) 
	{
		memset(head, -1, sizeof(head));
		tot = 0;
		for (int i = 2; i <= n; i++) 
		{
			scanf_s("%d%d", &v, &w);
			add(i, v, w);
			add(v, i, w);
		}
		d1[x] = 0;
		dfs(1, -1, d1);
		d1[x] = 0;
		dfs(x, -1, d1);
		d2[x] = 0;
		dfs(x, -1, d2);
		for (int i = 1; i <= n; i++)
			printf("%d\n", max(d1[i], d2[i]));
	}
	return 0;
}

Published 32 original articles · Likes0 · Visits 681

Guess you like

Origin blog.csdn.net/qq_43814559/article/details/105253234