A job week6

Meaning of the questions:
lab originally have 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. We can calculate the same numbers 4 and 5, the computer PC answer is 4.
INPUT:
the input file contains multiple test data. 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:
For each set of test data output line N, the number i represents the i-th row computer answers (. 1 <= i <= N).
Sample INPUT:
. 5
. 1. 1
2. 1
. 3. 1
. 1. 1
Sample output:
. 3
2
. 3
. 4
. 4
ideas:
Meaning of the questions are solved by using a method known tree. , Traversed by a forward dfs star storage side. The first time through it to start the search node v1 from the point farthest from any, the second pass, from the point to start the search node v1 v2 from the farthest point, the diameter of the distance between v1 and v2 is tree. V2 from the third pass of the search start point, because during the traversal, the second pass each point of the stored distance v1 point, the third pass each point of the distance to the point v2 stored by comparing the size of the , the distance from each point and v1, v2, the distance will be far away from the stored array, obtains the i-th computer with other computers from the maximum length of the cable and outputs.

Code:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
	int begin;
	int end;
	int value;
	int next;
}temp[100050];
int cm=1;
int a[100050];
int n;
int vis[100050];
int bm[100050];
int dm[100050];
void init(int &n)
{
	//cm=1;
	cm=0;
	for(int i=0;i<=n;i++)
	{
		a[i]=-1;
		vis[i]=0;
		bm[i]=0;
		dm[i]=0;
	}
 } 
void edge(int begin,int end,int value)
{
	temp[cm].begin=begin;
	temp[cm].end=end;
	temp[cm].value=value;
	temp[cm].next=a[begin];
	a[begin]=cm;
	cm++;
}

int v1=0;
int v2=0;
void dfs1(int begin)
{
	vis[begin]=1;
	for(int i=a[begin];i!=-1;i=temp[i].next)
	{
		if(bm[temp[i].end]==-1)
		//if(!vis[temp[i].end])
		{
			vis[temp[i].end]=1;
			bm[temp[i].end]=bm[temp[i].begin]+temp[i].value;
			if(bm[temp[i].end]>bm[v1])
			{
				v1=temp[i].end;
			}
			dfs1(temp[i].end);
		}
	}
}
void dfs2(int begin)
{
	vis[begin]=1;
	for(int i=a[begin];i!=-1;i=temp[i].next)
	{
		if(bm[temp[i].end]==-1)
		//if(!vis[temp[i].end])
		{
			vis[temp[i].end]=1;
			bm[temp[i].end]=bm[temp[i].begin]+temp[i].value;
			if(bm[temp[i].end]>bm[v2])
			{
				v2=temp[i].end;
			}
			dfs2(temp[i].end);
		}
	 } 
}
void dfs3(int begin)
{
	vis[begin]=1;
	for(int i=a[begin];i!=-1;i=temp[i].next)
	{
		if(dm[temp[i].end]==-1)
		//if(!vis[temp[i].end])
		{
			vis[temp[i].end]=1;
			dm[temp[i].end]=dm[temp[i].begin]+temp[i].value;
			if(dm[temp[i].end]>bm[temp[i].end])
			{
				bm[temp[i].end]=dm[temp[i].end];
			}
			dfs3(temp[i].end);
		}
	 } 
}
int main()
{
	int end;
	int value;
	while(cin>>n)
	{
		init(n);
		for(int i=2;i<=n;i++)
		{
			cin>>end>>value;
			edge(end,i,value);
			edge(i,end,value);
		}
		for(int i=0;i<=n;i++)
		{
			bm[i]=-1;
		}
		bm[1]=0;
		dfs1(1);
		for(int i=0;i<=n;i++)
		{
			bm[i]=-1;
			//bm[i]=0;
		}
		bm[v1]=0;
		dfs2(v1);
		for(int i=0;i<=n;i++)
		{
			dm[i]=-1;
			//dm[i]=0;
		}
		dm[v2]=0;
		dfs3(v2);
                for(int i=1;i<=n;i++)
		{	cout<<bm[i]<<endl;
		}
	}
	return 0;
}
Published 19 original articles · won praise 0 · Views 204

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/105333245