The Magic Apple Treetime limit:3000/1000ms (java/other) Memory limit:65535/32768k (Java/other) total submission (s): 26 Accepted Submission (s): 2Problem descriptionsailormoon girls all like eating many kinds of fruit, such as banana, grape, Apple and so On.<br>one day, when they is walking on a orchard, they found a magic apple tree. The Magic apple tree has many nodes,but there is only one root. Each notes have its label. It is labeled from 1.On to the first day,only each leaf nodes (with no children nodes) has apples. Any other nodes has no apples. The number of apples so each leaf nodes has is just the label of this node. When all the immediate children of a node have apples,this node would grow some Apple on the next day. If a node has K immediate children node,the number of Apple it this node grow on next day is just the number of apples t Hat The (K + 1)/2th smaller node has. The Xth smaller node means there is x–1 nodes ' number of apples is less than this node ' s Number of Apple.<br>now you task was to calculate the number of apples that the root have at last.
Inputthere is multiple test Cases.<br>each case contains a positive integer n, it means this tree has N nodes, Labe Led 1, 2, ... N (0 < n <= 20000). <br>the following n lines describe the children of all nodes in order of their labels. The (X + 1) th line in each test case starts with a number p (0 <= P <n), it means the Xth node have p immediate child ren nodes.then followed by P positive integers, means the label of immediate child node
Outputprint the number of apples, the root grow at last.
Sample Input72 2 5 6 70000 123 2 3 402 5 7 8 x 120000000 sample Output46 Simple test instructions: There are n nodes in the tree, each node has child nodes, the first day leaf node knot Fruit, number equals number, the next day, other non-leaf nodes only start the result, for non-leaf nodes, assuming that there is a K child node, then he received the fruit for the child node (K + 1)/2 large number, the root node. Thinking Analysis: The beginning of the topic are difficult to understand, and then has been time-out,,, with the input plug-in can be ... DFS search:
# include <iostream># include<fstream># include<cstring># include<cstdio># include<algorithm># include<vector>using namespaceStd;vector<int> map[20001];int is[20001], father[20001];intDfsint);CharC//online Check the input plugInlinevoidFint&x) { while(c = GetChar (), C <'0'|| C >'9'); X= C-'0'; while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0';}intMain () {//freopen ("Aaa.txt", "R", stdin); intN, t; while(SCANF ("%d", &n) = =1) {memset ( is,0,sizeof( is)); for(inti =1; I <= N; i++) map[i].clear (); for(inti =1; I <= N; i++) {f (father[i]);//parent node of a direct child node for(intj =0; J < Father[i]; J + +) {f (t); Map[i].push_back (t); is[T] =1;//determine the root node, } } intRoot; for(inti =1; I <= N; i++ ) if( is[I] = =0) Root=i; printf ("%d\n", DFS (root)); } return 0;}intDfsintroot) {Vector<int>Tep; if(Father[root] = =0)//returns the number directly when the child node is 0 o'clock returnRoot; for(inti =0; i < father[root]; i++) Tep.push_back (DFS (map[root][i)); Sort (Tep.begin (), Tep.end ()); returntep[(Father[root] +1) /2-1];}
HDU Search Practice the Magic apple tree