Codeforces 743d:chloe and pleasant prizes (tree-shaped DP)

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/743/D

Test instructions: The point weights for the disjoint subtree of the maximum of two, and if there are not two disjoint subtrees, the output impossible.

Thought: Before it seems to have done this type of topic Ah, know is the tree-like DP, but do not know how to ensure that two do not intersect. After looking at someone else's code,

At the time of Dfs backtracking,

1 voidDfsintUintFA) {2Sum[u] =W[u];3      for(inti = Head[u]; ~i; i =edge[i].nxt) {4         intv =edge[i].v;5         if(v = = FA)Continue;6 Dfs (v, u);7Sum[u] + =Sum[v];8         if(Dp[u] >-inf) ans = max (ans, dp[u] +dp[v]);9Dp[u] =Max (Dp[u], dp[v]);Ten     } OneDp[u] =Max (Dp[u], sum[u]); A}

If the first execution of the IF statement, you can guarantee that only the U node as the root, its subtree has two or more than two, otherwise it will not update ans. And this time DP "U" is still only a sub-tree currently swept the maximum weight and, plus a sub-large DP "V", so that it is guaranteed to be the largest two disjoint subtree weights and. After traversing the DP "U" must be the maximum weight of all subtrees with the root of U, and then the sum "U" update whether to include the weights of this node.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <string>6#include <cmath>7#include <queue>8#include <vector>9 using namespacestd;Ten #defineN 200010 OnetypedefLong LongLL; A ConstLL INF = 1ll<< -; - structnode - { the     intV, NXT; -}edge[n*2]; - LL Dp[n], sum[n], w[n], head[n], tot, ans; -  + voidAddintUintv) { -EDGE[TOT].V = v; EDGE[TOT].NXT = Head[u]; Head[u] = tot++; + } A  at voidDfsintUintFA) { -Sum[u] =W[u]; -      for(inti = Head[u]; ~i; i =edge[i].nxt) { -         intv =edge[i].v; -         if(v = = FA)Continue; - Dfs (v, u); inSum[u] + =Sum[v]; -         if(Dp[u] >-inf) ans = max (ans, dp[u] +dp[v]); toDp[u] =Max (Dp[u], dp[v]); +     } -Dp[u] =Max (Dp[u], sum[u]); the } *  $ intMain ()Panax Notoginseng { -     intN; theCIN >>N; +Memset (Head,-1,sizeof(head)); A      for(inti =1; I <= N; i++) Cin >>W[i]; the      for(inti =1; I < n; i++) { +         intu, v; -CIN >> U >>v; $ Add (U, v); Add (v, u); $     } -Ans =-INF; -      for(inti =1; I <= N; i++) Dp[i] =-INF; theDfs1, -1); -     if(Ans <=-inf) puts ("Impossible");Wuyi     Elsecout << ans <<Endl; the     return 0; -}

Codeforces 743d:chloe and pleasant prizes (tree-shaped DP)

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.