標籤:style blog color os io for cti div
給你一顆邊帶權值的樹,求樹上的每一點距離其最遠的一個點的距離
比較典型的題了,主要方法是進行兩次DFS,第一次DFS求出每一個點距離它的子樹的最遠距離和次遠距離,然後第二次DFS從父節點傳過來另一側的樹上的距離它的最遠距離進行一次比較便可得出任意點的最遠距離了
之所以需要記錄最遠和次遠是為了辨別父節點的最遠距離是否是根據自己得來,如果是的話應該選擇父節點的次遠距離,保證結果的準確性
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 }