Codeforces beta round #69 (Div. 2 only) e question

Source: Internet
Author: User

Given a tree, each node has a weight. Given the starting point a, starting from, when each vertex is reached, a weight value of the vertex is obtained (the node is reduced by one) and the maximum number of values can be obtained...

Analysis: DFS increases the value of the root node by 1, and then performs the same operation on each subtree. Each time you select a subtree, you may get more priority options, at last, the parent node and the child node may have surplus and can be moved back and forth...
It is a tree-like DP .. Amount...

Code: well-written... Because a few int64s are missing, WA has a large film...

# Include <iostream> # include <stdio. h >#include <cmath> # include <vector> # include <algorithm> using namespace STD; const int n = 100010; struct node {int P; _ int64 A, num; // maximum possibility of num subtree} TMP; int N, flag [N] ;__ int64 A [n], ans; vector <node> B [N]; int CMP (const node & A, const node & B) {return. num> B. num ;:__ int64 DFS (INT p) {vector <node >:: iterator it; int CNT = 0; For (IT = B [p]. begin (); it! = B [p]. end (); It ++) {If (flag [(* it ). p] = 0) {flag [(* it ). p] = 1; (* it ). num = DFS (* it ). p); flag [(* it ). p] = 0; (* it ). A = A [(* it ). p]; CNT ++ ;}}if (CNT = 0) {return 0 ;}sort (B [p]. begin (), B [p]. end (), CMP); _ int64 num = 0; A [p] --; For (IT = B [p]. begin (); A [p]> 0 & it! = B [p]. end () & (* it ). num> 0; it ++) {If (flag [(* it ). p] = 0) // (* it ). A is always greater than or equal to 1 .. {(* It ). A --; A [p] --; num + = (* it ). num + 2 ;}__ int64 num1 = 0; For (IT = B [p]. begin (); it! = B [p]. end (); It ++) {If (flag [(* it ). p] = 0 & (* it ). a> 0) {num1 + = (* it ). A ;}} num + = min (_ int64) A [p], num1) * 2; // A [p]-= min (_ int64) A [p], num1); // reversed... A [p] ++; return num;} int main () {int I, J, K, head; while (scanf ("% d", & N )! = EOF) {for (I = 1; I <= N; I ++) {scanf ("% i64d", & A [I]); flag [I] = 0 ;}for (I = 1; I <= n-1; I ++) {scanf ("% d", & J, & K ); TMP. P = K; TMP. A = A [k]; TMP. num = 0; B [J]. push_back (TMP); TMP. P = J; TMP. A = A [J]; TMP. num = 0; B [K]. push_back (TMP);} scanf ("% d", & head); A [head] ++; flag [head] = 1; ans = DFS (head ); printf ("% i64d \ n", ANS); for (I = 1; I <= N; I ++) {While (! B [I]. Empty () {B [I]. pop_back () ;}} return 0 ;}

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.