[Luo Gu P4107] HEOI2015 bunny and cherry blossoms

Problem Description

Before long, long time, the forest is home to a group of rabbits. One day, the rabbits suddenly decided to see cherry blossoms. Rabbits cherry trees where the forest is very special. Cherry trees by n branches diverging points, numbered from 0 to n-1, n bifurcation points which are connected by the n-1 branches, we can see it as a rooted tree structure in which nodes are 0 root node. Each node of this tree will have some cherry, of which the i-th node has c_i cherry flowers. Each node has the largest cherry tree load m, for each node i, its son nodes and the number of node i can not exceed the number of cherry and m, i.e., son (i) + c_i <= m, son wherein (i) represents the number of son i, if i is a leaf node, the son (i) = 0

Now I think too many rabbits cherry tree node, hoping to remove some nodes. When a node is removed, cherry and its son nodes of the nodes are connected to the parent node of the deleted node. If the parent node is also deleted, it will continue to connect up, up until the first node is not deleted.

Now we want to calculate in rabbits without violating the maximum load of up to delete the number of nodes.

Note that the root can not be deleted, the deleted node is not included in the load.

Input Format

The first line of the input two positive integers, n and m each represent the number of nodes and the maximum load

The second line n integers c_i, cherry tree represents the number of nodes on the i-th

Next n lines of the first number indicates the number of son k_i this node, the next node k_i represents an integer number of son

Output Format

Line An integer representing the largest number of nodes can be deleted.

Sample input

10 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
0

Sample Output

4

data range

For 30% of the data, 1 <= n <= 5000, 1 <= m <= 100, 0 <= c_i <= 100

For 70% of the data, 1 <= n <= 200000, 1 <= m <= 2000, 0 <= c_i <= 1000

To 100% of the data, 1 <= n <= 2000000, 1 <= m <= 100000, 0 <= c_i <= 1000

To ensure that the initial data, each node number to the number of cherry son of the node is greater than 0 and not exceeding m

Resolve

Can be found, how to delete a node does not affect the node's ancestors. So, we can traverse the nodes in sequence from bottom to top. Set \ (w [i] = son [i] + c [i] \) means to delete a price point that will increase his father \ (w [i] \) of load. So, greedy strategy is a consideration of small to large deletion for each point can not be deleted until his son so far. Delete and update the cost of the node.

The question is why it is right to do so. Following is a brief discussion about whether or not this greedy strategy have after-effect. If you delete a point because his son become too bloated, we can choose not to delete deleted him to change his cost of smaller siblings, did not have an impact on the optimal answer. So, there is no after-effect.

Code

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define N 2000002
using namespace std;
vector<int> v[N];
int head[N],ver[N*2],nxt[N*2],l;
int n,m,i,j,c[N],son[N],w[N],ans;
int read()
{
    char c=getchar();
    int w=0;
    while(c<'0'||c>'9') c=getchar();
    while(c<='9'&&c>='0'){
        w=w*10+c-'0';
        c=getchar();
    }
    return w;
}
void insert(int x,int y)
{
    l++;
    ver[l]=y;
    nxt[l]=head[x];
    head[x]=l;
}
int my_comp(const int &x,const int &y)
{
    return w[x]<w[y];
}
void dfs(int x)
{
    w[x]=son[x]+c[x];
    for(int i=head[x];i;i=nxt[i]){
        dfs(ver[i]);
        v[x].push_back(ver[i]);
    }
    sort(v[x].begin(),v[x].end(),my_comp);
    for(unsigned int i=0;i<v[x].size();i++){
        int y=v[x][i];
        if(w[x]+w[y]-1<=m){
            ans++;
            w[x]+=w[y]-1;
        }
        else break;
    }
}
int main()
{
    n=read();m=read();
    for(i=1;i<=n;i++) c[i]=read();
    for(i=1;i<=n;i++){
        son[i]=read();
        for(j=1;j<=son[i];j++){
            int x=read()+1;
            insert(i,x);
        }
    }
    dfs(1);
    printf("%d\n",ans);
    return 0;
}

Tips

Greedy strategy is to delete the youngest son of the cost for each point from the bottom up until it can not be deleted.

In fact, I have been completely thought greedy strategy, but this strategy is mistaken aftereffect in nature, because the losses will not be greater than the benefits derived. Questions about aftereffect of, conclusions need to think carefully about before the next confirmation, not rough seat of your pants.

Guess you like

Origin www.cnblogs.com/LSlzf/p/11746239.html