XMU1456 water body

Source: Internet
Author: User

1456. squirrel harvest Time Limit: 2000 MS Memory Limit: 65536 KTotal Submissions: 64 (23 users) Accepted: 21 (20 users) [My Solution] Description Squirrel lives in a Tree Containing n + 1 nodes. It lives in node 0. Of course, to survive, it will go out to collect results every day. To simplify the problem, we assume that pine nuts only appear on n other nodes except node 0. Squirrel starts from node 0 every day and returns a pine nut to a node, on the way back from the original road, take each passing node to pick up one pine cones (you can choose not to pick them ). So, Squirrel wants to know how many days later the fruit on the tree will be fully lit by it? The first line of Input has a positive integer n (n <= 50,000), and the next line has n integers (1 <= I <= n, 1 <= ai <= 20,000). the I-th number indicates that the fruit number on the node numbered I is ai, followed by n rows, each row has two integers x, y (0 <= x, y <= n) indicates that two nodes are connected by an edge. The input ensures that any two points in the given graph have only one path. Output outputs an integer that represents the number of days required. Sample Input3 3 2 2 0 1 1 2 3 3 Sample Output4Sourcedoraemon @ xmu [Submit] [Statistic] [Status] [Discuss] http://acm.xmu.edu.cn/JudgeOnline/problem.php? Id = 1456 question: A tree with n nodes starts from node 0 to each node to fetch fruit. Each time a tree with n nodes picks up a fruit and returns, the fruit of a passing node can also be picked. how many times can all the fruit on the tree be taken: the minimum number of times required for each node of a branch depends on the sum of the current node and all its subnodes. [cpp] # include <stdio. h ># include <vector> using namespace std; vector <int> a [50000 + 100]; int val [50000 + 100], vis [50000 + 100]; int DFS (int k) {if (vis [k]) return 0; vis [k] = 1; if (a [k]. size () = 0) return val [k]; int sum = 0; for (int I = 0; I <a [k]. size (); I ++) sum + = DFS (a [k] [I]); Return val [k]> sum? Val [k]: sum;} int main () {int n, I, x, y, ans; while (scanf ("% d", & n )! = EOF) {for (I = 0; I <50000 + 10; I ++) {a [I]. clear (); val [I] = 0; vis [I] = 0;} for (I = 1; I <= n; I ++) scanf ("% d", & val [I]); for (I = 1; I <= n; I ++) {scanf ("% d ", & x, & y); a [x]. push_back (y); a [y]. push_back (x);} ans = DFS (0); printf ("% d \ n", ans);} 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.