Computer

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
找一个出距离每个点最远的距离并输出    如果在求端点的时候直接找会超时    可以用打表分别从两个端点出发  记录每个点到端点的距离     输出的时候比较到两个端点的距离  输出最大的即可
<pre name="code" class="cpp">#include<cstdio>     
#include<queue>
#include<cstring>
using namespace std;
struct node 
{
	int from,to,val,next;
};
int zuo,you;
node dian[1000001];
int cut;
int head[100001];
int zuod[100001];
int youd[100001];
void chushihua()
{
	cut=0;
	memset(head,-1,sizeof(head));
	memset(zuod,0,sizeof(zuod));
	memset(youd,0,sizeof(youd));
}

void  Edge(int u,int v,int w)
{
	dian[cut].from =u;
	dian[cut].to =v;
	dian[cut].val =w;
	dian[cut].next =head[u];
	head[u]=cut++;
}
int jilu;
int vis[100001];
int dist[100001];
int dis[100001];
int ans;
int n,m,d;
void  bfs(int s)
{
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
	ans=0;
	queue<int>q;
	q.push(s);vis[s]=1;dist[s]=0;
	while(!q.empty() )
	{
		int u=q.front() ;
		q.pop() ;
		for(int i=head[u];i!=-1;i=dian[i].next )
		{
			int v=dian[i].to ;
			if(!vis[v])
			{
				if(dist[v]<dist[u]+dian[i].val )
				dist[v]=dist[u]+dian[i].val ;
					vis[v]=1;
		            q.push(v); 	
			}
		}
     }
	for(int i=1;i<=n;i++)
	{
			if(ans<dist[i])
		     {
			    ans=dist[i];
			    jilu=i;
	          }
	}
		
}
int main()
{


	while(scanf("%d",&n)!=EOF) 
	{
		chushihua();
		for(int i=2;i<=n;i++)
		{
			scanf("%d%d",&m,&d);
			Edge(i,m,d);
			Edge(m,i,d);
		}
	
		bfs(1);	
		zuo=jilu;//假设为左端点
		bfs(zuo);//从左端点往右端点扫	you=jilu;//假设为右端点
		for(int i=1;i<=n;i++)
		{
			zuod[i]=dist[i];//记录左端点到每个点的距离
		}
		bfs(you);//从右端点往左端点扫
		for(int i=1;i<=n;i++)
		{
			youd[i]=dist[i];记录右端点到每个点的距离
		}
	  for(int i=1;i<=n;i++)
	  {
	  		printf("%d\n",max(zuod[i],youd[i]));
	  }
	
	}
	return 0;
}



posted @ 2019-12-12 09:00  千金一发  阅读(522)  评论(0编辑  收藏  举报