Give you a tree with the edge weight, and find the distance between each point on the tree and its farthest vertex.
This is a typical question. The main method is to perform two DFS operations. For the first time, the DFS calculates the maximum distance and maximum distance between each vertex and Its subtree, then, the second DFS will be transferred from the parent node to the tree on the other side for a comparison between the longest distance and the longest distance of the point.
To identify whether the maximum distance of the parent node is obtained by yourself, select the maximum distance of the parent node to ensure the accuracy of the result.
1 //#pragma comment(linker,"/STACK:102400000,102400000") 2 #include <map> 3 #include <set> 4 #include <stack> 5 #include <queue> 6 #include <cmath> 7 #include <ctime> 8 #include <vector> 9 #include <cstdio>10 #include <cctype>11 #include <cstring>12 #include <cstdlib>13 #include <iostream>14 #include <algorithm>15 using namespace std;16 #define INF 1e817 #define inf (-((LL)1<<40))18 #define lson k<<1, L, mid19 #define rson k<<1|1, mid+1, R20 #define mem0(a) memset(a,0,sizeof(a))21 #define mem1(a) memset(a,-1,sizeof(a))22 #define mem(a, b) memset(a, b, sizeof(a))23 #define FOPENIN(IN) freopen(IN, "r", stdin)24 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)25 template<class T> T CMP_MIN(T a, T b) { return a < b; }26 template<class T> T CMP_MAX(T a, T b) { return a > b; }27 template<class T> T MAX(T a, T b) { return a > b ? a : b; }28 template<class T> T MIN(T a, T b) { return a < b ? a : b; }29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }31 32 //typedef __int64 LL;33 //typedef long long LL;34 const int MAXN = 10005;35 const int MAXM = 20005;36 const double eps = 1e-13;37 //const LL MOD = 1000000007;38 39 int N;40 int head[MAXN], next[MAXM], tot;41 int u[MAXM], v[MAXM], w[MAXM];42 int fir[MAXN], sec[MAXN], ans[MAXN];43 44 void addEdge(int U, int V, int W)45 {46 u[tot] = U;47 v[tot] = V;48 w[tot] = W;49 next[tot] = head[U];50 head[U] = tot;51 tot ++;52 }53 54 int dfs1(int x, int fa)55 {56 fir[x] = sec[x] = 0;57 for(int e = head[x]; e != -1; e = next[e]) if(v[e] != fa)58 {59 int dis = dfs1(v[e], x) + w[e];60 if(dis >= fir[x]) { sec[x] = fir[x]; fir[x] = dis; }61 else if(dis > sec[x]) sec[x] = dis;62 }63 return fir[x];64 }65 66 void dfs2(int x, int fa, int dis)67 {68 ans[x] = MAX(fir[x], dis);69 for(int e = head[x]; e != -1; e = next[e]) if(v[e] != fa)70 {71 int y = v[e];72 if(fir[y] + w[e] == fir[x])73 dfs2(y, x, MAX( dis, sec[x]) + w[e] );74 else75 dfs2(y, x, MAX( dis, fir[x]) + w[e] );76 }77 }78 79 int main()80 {81 82 while(~scanf("%d", &N))83 {84 tot = 0;85 mem1(head);86 int V, W;87 for(int i = 2; i <= N; i ++)88 {89 scanf("%d %d", &V, &W);90 addEdge(i, V, W);91 addEdge(V, i, W);92 }93 dfs1(1, 1);94 dfs2(1, 1, 0);95 for(int i = 1; i <= N; i ++ )96 printf("%d\n", ans[i]);97 }98 return 0;99 }