Hangzhou Electric Oj_hdu3290_the Magic apple tree

Source: Internet
Author: User

Title: Give an apple tree with N (0<n<=20000) nodes, the tree has only 1 root (root node), each node has 1 label (number), label from 1 to N. Each node of the apple tree grows apples according to the following rules:

1. The number of apples grown on the leaf node equals the label of the leaf node.

2. A Father node has a K-son node, until its K-son node grows out of the apple, the Father node began to grow apples. The number of apples grown out of the father's node equals the number of apples in all of its sons (k+1)/2 small sons nodes.

The number of apples grown by the root node is obtained.

To give a chestnut: as shown, the node of label 4,5,6,7 is the leaf node, according to rule one, the number of apples that the leaf node grows is their number, then they grow the number of apples is 4,5,6,7 respectively. Next, the node 2,3 all the child nodes have grown out of the apple, according to rule two, the turn node 2,3 grow apples. Node 2 has 2 sub-nodes, according to the calculation: (2+1)/2 = 1, the number of apples in Node 2 equals the number of apples in the 1th small son node of all its sons nodes, that is, the number of apples equal to node 4. Similarly, the number of apples in node 3 is 6. Finally calculate the number of apples on Node 1.

1. Use the adjacency table to store the tree.

2, the visit array to record the degree of each node, that is, the side of the root node found, the root node in the degree of 0.

3, according to the rules of the topic, design DFS Search to find the number of apples per node.

4, with priority queue to maintain the number of sub-nodes (k+1)/2 small apple.

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <queue>5#include <functional>6#include <vector>7 using namespacestd;8 9 Const intN =20010;Ten  One /** child node structure **/ AtypedefstructLeafnode - { -     intLabel///node Label the     intNext///Next child node address - }leafnode; -  - /** Parent Node structure **/ +typedefstructNode - { +     intLabel///node Label A     intSmaller///record the little son of a few at     intNext///son node address - }node; -  -Node Tree[n];///adjacency Table Store apple tree - intVisit[n];///record the entry of a node -Leafnode Leaf[n];///static array Storage leaf node in intLeafindex;///record static array next assignable address - intN///Number of nodes to  + /** Number of apples on the DFS Search root node **/ - intDfsintroot) the { *     ///The root node is a leaf node and returns the number of apples directly, which is the label $     if( -1==tree[root].next)Panax Notoginseng         returnTree[root].label; -  the     ///The root node is the parent node, which calculates the number of apples for all child nodes of the node, and then returns +     intp =Tree[root].next; Apriority_queue<int,vector<int>,less<int> > Q;///use priority queue to maintain the number of small sub-nodes (k+1)/2 apples the  +      while( -1!=p) -     { $ Q.push (Dfs (Leaf[p].label)); $          while(int(Q.size ()) > Tree[root].smaller)///The number of elements in the priority queue is more than (k+1)/2, which means that the team's first place is not the result we need - Q.pop (); -p =Leaf[p].next; the     } - Wuyi     returnQ.top ();///The first priority queue is the result. the } -  Wu /** Initialization function **/ - voidInitvoid) About { $Leafindex =0; -memset (Visit,0,sizeof(visit)); - } -  A /** Add a child node numbered as label to the adjacency table of the parent node numbered I **/ + voidAddNode (intLabelinti) the { -visit[label]++; $     intTMP = leafindex++; theLeaf[tmp].label =label; theLeaf[tmp].next =Tree[i].next; theTree[i].next =tmp; the } -  in intMainvoid) the { the     inti,j, label; About      while(SCANF ("%d", &n)! = EOF)///N Nodes the     { the init (); the          for(i =1; I <= N; ++i) +         { -scanf"%d", &j); theTree[i].label = i;///Initialize node IBayiTree[i].smaller = (j+1)/2; theTree[i].next =-1; the              while(j--)///J Child receiving I-node -             { -scanf"%d", &label); the addNode (label, i); the             } the         } the          for(i =1; I <= N; ++i) -         { the             if(0= = Visit[i])///the node with the degree of 0 is the root node, and the search starts from the root node the             { theprintf"%d\n", DFS (i));94                  Break; the             } the         } the     }98     return 0; About}

Hangzhou Electric Oj_hdu3290_the Magic apple tree

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.