HDU 2196 tree DP Computer

Source: Internet
Author: User

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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.