https://cn.vjudge.net/contest/312854#problem/A
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 1Sample Output
3 2 3 4 4
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
//每一个点的最大距离一定是到达直径上的某一个端点
struct node{
ll to;
ll w;
};
vector<node>p[10001];
ll dis[10001];
ll max_len,s;
//len是当前点距离起始点的距离
void dfs(ll u,ll fa,ll len){
//如果max_len<len说明还能延伸
//直到找到不能延伸的点
//该点就是树的直径的端点之一
if(max_len<=len){
s=u;//最终记录下一个直径端点
max_len=len;
}
ll Len=p[u].size();
for(ll i=0;i<Len;i++){
ll v=p[u][i].to;
if(v==fa)continue;
dfs(v,u,len+p[u][i].w);
//更新v点到起始点的最长距离
dis[v]=max(dis[v],len+p[u][i].w);
}
}
int main(){
ll n;
while(cin>>n){
for(ll i=0;i<=n;i++){
p[i].clear();
}
for(ll i=2;i<=n;i++){
ll y,w;
cin>>y>>w;
node t;
t.to=y;
t.w=w;
p[i].push_back(t);
t.to=i;
p[y].push_back(t);
}
memset(dis,0,sizeof(dis));
max_len=0;
s=0;
dfs(1,-1,0);//找到直径上的某一端点
//将该端点作为新的起始点进行dfs并找到另一个直径端点
//在这过程中更新每一点到该端点的最远距离
dfs(s,-1,0);
//在从第二个端点作为起始点进行dfs
//此过程目的只是为了更新每一点到该端点的最远距离
dfs(s,-1,0);
for(ll i=1;i<=n;i++){
cout<<dis[i]<<endl;
}
}
return 0;
}
// /\ | / |**、
// / \ | / | \
// / \ |/ | / _____ ____ | /
// /------\ |\ |__/ / \ \ /\ / / \ | /
// / \ | \ | / \ \ / \ / /______\ |/
// / \ | \ | \ / \ / \ / \ |
// / \ | \ | \_____/ \/ \/ \_____ |
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//