[A] Week6 work with krypton Kim Dong

Title Description

Original lab with a computer (No. 1), recently krypton gold band division Cuckoo East but also for the purchase of laboratory N-1 computers, numbered 2 to N. Every computer connected to a computer with a previously installed cable. But Cuckoo East worry speed is too slow, he wanted to know the i-computer network cable to a maximum length of other computers, but poor cuckoo East just recently suffered a drop of wisdom against cosmic rays, please help him.
Here Insert Picture Description
Note: this figure corresponding to the input sample, from this figure you can see, number 1 farthest from the computer is a PC computer 4, the distance between them is 3. No. 4 and No. 5 PC computers are farthest from the point of the computer 2, so the answer is No. 2.5 No. 3 farthest from the computer PC, the computer 3 is so for its answer is 3. The same we can calculate the number 4 and No. 5 PC computer answer is 4.

Input Format

Input file contains several test cases. For each test, the first row of an integer N (N <= 10000), there are N-1 the next row, each row two numbers, for the i-th row of the two numbers that represent the number of computers connected to the i computer number and length of the cable between them. The total length of the cable does not exceed 10 ^ 9, separated by a space between each number.

Output Format

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

Sample Input

5
1 1
2 1
3 1
1 1

Sample Output

3
2
3
4
4

Thinking

The title computer can imagine point, imagine line with edge weights (a weight of the distance), this problem will be converted into A weighted undirected FIGS any one point to its distance from the farthest point, and by the figure shows that Italy is a tree.
The next question is how the tree from a point farthest point demand, first of all, we have to understand the concept of the diameter of the tree, that tree from the farthest distance between two points, may permit the tree to a certain point most far point will be one of the two end points of the diameter of the tree, and therefore, we can reverse the two endpoints strike tree diameter to distance between each point, whichever is the largest, that is, the distance from the furthest point of the respective points value.
So, we just a point of departure from DFS, find the farthest point will be for one endpoint, DFS performed again starting from the endpoint, while recording their distance to each point, and find the farthest point, as the other endpoint, starting again from the other end were DFS, if the distance between the point recorded before a certain point distance greater than the distance value is updated, after these three traversal, we recorded a distance, that is, each point to the farthest point the distance.

Code

#include <iostream>
#include <vector>
#include<string.h> 
using namespace std;
const int size=1e4+10; 
vector<pair<int,int>> a[size];
int Max[size];
bool vis[size];
int far;
void dfs(int start,int length)
{
	vis[start]=true;
	for(int i=0;i<a[start].size();i++)
	{
		int temp=a[start][i].first;
		if(!vis[temp])
		{
			Max[temp]=max(Max[temp],length+a[start][i].second);
			far=Max[far]<Max[temp]?temp:far;
			dfs(a[start][i].first,length+a[start][i].second);
		}	
	}
}
int main(int argc, char** argv) {
	int n;
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
			a[i].clear();
		memset(Max,0,sizeof(Max));
		memset(vis,0,sizeof(vis));
		for(int i=2;i<=n;i++)
		{
			int e1,e2;
			scanf("%d%d",&e1,&e2);
			a[i].push_back({e1,e2});
			a[e1].push_back({i,e2});
		}
		far=1;
		dfs(1,0);
		memset(vis,0,sizeof(vis));
		dfs(far,0);
		memset(vis,0,sizeof(vis));
		dfs(far,0);
		for(int i=1;i<=n;i++)
			printf("%d\n",Max[i]);
	}
	return 0;
}
Published 24 original articles · won praise 8 · views 523

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/105161101
Kim