Link: HDU 2196 Computer
Analysis: first, obtain the maximum distance from any point to another point, and then update its neighbor point with this point as the center,
Use the updated vertex to update the adjacent vertex ...... so on!
Code:
# Include <iostream> # include <cstdio> # include <cmath> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <iomanip> using namespace std; const int inf = 0x7FFFFFFF; const int maxn = 11111; struct node {int to, dix, sum; node * next;} tree [maxn <1], * head [maxn]; int ptr, n; bool vis [maxn]; int dp [maxn], h [maxn]; void Init () {ptr = 1; memset (dp, 0, sizeof (dp); memset (vis, False, sizeof (vis); memset (head, 0, sizeof (head);} void AddEdge (int x, int y, int s) {tree [ptr]. dix = s; tree [ptr]. to = y; tree [ptr]. next = head [x]; head [x] = & tree [ptr ++];} void DFS (int cnt) {vis [cnt] = true; node * p = head [cnt]; while (p! = NULL) {if (vis [p-> to]) {p = p-> next; continue;} DFS (p-> ); dp [cnt] = max (dp [cnt], dp [p-> to] + p-> dix ); p-> sum = dp [p-> to] + p-> dix; p = p-> next;} void Tree_Dp (int father, int son) {if (vis [son]) return; vis [son] = true; int Max = 0; node * p = head [father]; while (p! = NULL) {if (p->! = Son) Max = max (Max, p-> sum); p = p-> next;} p = head [son]; while (p! = NULL) {if (p-> to = father) {p-> sum = p-> dix + Max; break;} p = p-> next ;} p = head [son]; while (p! = NULL) {dp [son] = max (dp [son], p-> sum); Tree_Dp (son, p-> ); p = p-> next;} int main () {while (~ Scanf ("% d", & n) {Init (); for (int I = 2; I <= n; ++ I) {int, b; scanf ("% d", & a, & B); AddEdge (a, I, B); AddEdge (I, a, B );} DFS (1); // get the distance memset (vis, false, sizeof (vis) from 1 to all other points); node * p = head [1]; while (p! = NULL) {// update Tree_Dp (1, p-> to) from the neighboring point of 1; p = p-> next;} for (int I = 1; I <= n; ++ I) printf ("% d \ n", dp [I]);} return 0 ;} # include <iostream> # include <cstdio> # include <cmath> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <iomanip> using namespace std; const int inf = 0x7FFFFFFF; const int maxn = 11111; struct node {int to, dix, sum; node * next;} tree [maxn <1], * head [maxn]; int ptr, N; bool vis [maxn]; int dp [maxn], h [maxn]; void Init () {ptr = 1; memset (dp, 0, sizeof (dp )); memset (vis, false, sizeof (vis); memset (head, 0, sizeof (head);} void AddEdge (int x, int y, int s) {tree [ptr]. dix = s; tree [ptr]. to = y; tree [ptr]. next = head [x]; head [x] = & tree [ptr ++];} void DFS (int cnt) {vis [cnt] = true; node * p = head [cnt]; while (p! = NULL) {if (vis [p-> to]) {p = p-> next; continue;} DFS (p-> ); dp [cnt] = max (dp [cnt], dp [p-> to] + p-> dix ); p-> sum = dp [p-> to] + p-> dix; p = p-> next;} void Tree_Dp (int father, int son) {if (vis [son]) return; vis [son] = true; int Max = 0; node * p = head [father]; while (p! = NULL) {if (p->! = Son) Max = max (Max, p-> sum); p = p-> next;} p = head [son]; while (p! = NULL) {if (p-> to = father) {p-> sum = p-> dix + Max; break;} p = p-> next ;} p = head [son]; while (p! = NULL) {dp [son] = max (dp [son], p-> sum); Tree_Dp (son, p-> ); p = p-> next;} int main () {while (~ Scanf ("% d", & n) {Init (); for (int I = 2; I <= n; ++ I) {int, b; scanf ("% d", & a, & B); AddEdge (a, I, B); AddEdge (I, a, B );} DFS (1); // get the distance memset (vis, false, sizeof (vis) from 1 to all other points); node * p = head [1]; while (p! = NULL) {// update Tree_Dp (1, p-> to) from the neighboring point of 1; p = p-> next;} for (int I = 1; I <= n; ++ I) printf ("% d \ n", dp [I]);} return 0 ;}