[bzoj4027] [HEOI2015] rabbits and cherry blossoms

Source: Internet
Author: User

Title Description

A long long time ago, there lived a group of rabbits in the forest. One day, the rabbits suddenly decided to go to see the cherry blossoms. The cherry trees in the forest where the rabbits are located are very special. The cherry tree consists of n branches, numbered from 0 to n-1, the N fork points are connected by n-1 branches, we can think of it as a root tree structure, where node No. 0 is the root node. There will be some cherry blossoms on each node of the tree, and the node I has c_i cherry blossoms. Each node of the Cherry tree has the largest load m, for each node I, its son number of nodes and the number of cherry blossoms on the I node cannot exceed M, that is son (i) + c_i <= m, where son (i) represents the number of sons of I, if I is a leaf node, then son (i) = 0

Now the rabbits think there are too many nodes on the cherry tree, hoping to get rid of some of them. When a node is removed, the cherry blossom on the node and its son node are connected to the deleted node's parent node. If the parent node is also deleted, it will continue to connect up until the first node is not deleted. Now the rabbits want to calculate the maximum number of nodes that can be deleted without violating the maximum load. Note that the root node cannot be deleted and the deleted node is not counted into the payload.input

The first line enters two positive integers, and N and M represent the number of nodes and the maximum load

The second row n integer c_i, representing the number of cherry blossoms on the first node

Next n rows, the first number of each row k_i represents the number of sons of this node, and next K_i an integer representing the number of the son of this nodeOutput

An integer line representing the maximum number of nodes that can be deleted.

Sample Input10 4
0 2 2 2 4 1 0 4 1 1
3 6 2 3
1 9
1 8
1 1
0
0
2 7 4
0
1 5
0Sample Output4Tips

For 100% data, 1 <= n <= 2000000, 1 <= m <= 100000, 0 <= c_i <= 1000

Data guarantee Initial, each node number of cherry blossom and son node number of more than 0 and not more than M-tree dp+ greedy each time the selected node is deleted, the added weight is (the number of child nodes of the selected node + the number of cherry blossoms of the selected node)-1, then we can say the weight of each node as the number of child nodes + Cherry So there is greedy strategy: preferential selection of small sub-nodes, or if you choose other sub-nodes, then the node deleted by the nodes will not increase, and the weight of this node than the greedy scheme, the impact of the subsequent processing. First update the weight of the child nodes, and from small to large, according to the greedy strategy preferred to choose the weight of small, determine whether to remove, if you can remove the current node to update the weight. The time complexity is O (Nlogn), but the constant is small and can be too.
#include <stdio.h>#include<algorithm>#include<vector>using namespaceStd;vector<int> son[2000001];intc[2000001], M, ans;BOOLcmpintAintb) {    returnC[a] <c[b];}voiddpintx) {    inti;  for(i =0; I < (int) Son[x].size (); i + +) DP (Son[x][i]);    Sort (Son[x].begin (), Son[x].end (), CMP); C[X]+=son[x].size ();  for(i =0; I < (int) Son[x].size (); i + + )    {        if(C[x] + c[son[x][i])-1<=m) {c[x]+ = C[son[x][i]]-1; Ans++ ; }        Else             Break; }}intMain () {intN, I, K, X; scanf ("%d%d", &n, &m);  for(i =1; I <= N; i + +) scanf ("%d", &C[i]);  for(i =1; I <= N; i + +) {scanf ("%d", &k);  while(K--) {scanf ("%d", &x); Son[i].push_back (x+1); }} DP (1); printf ("%d\n", ans); return 0;}

[bzoj4027] [HEOI2015] rabbits and cherry blossoms

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.