P4107 [HEOI2015] Rabbit and cherry

Title 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 and output

Input # 1
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
Output # 1
4

Description / Tips

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

Thinking

greedy.

Because after each point will be deleted sons and cherry added to the father, so make c [i] + sons [i] is the number of nodes i delete price.

Each node is deleted in the cost of their children smallest one, then the cost of updating their look.

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=2000010;

int n,m,knt,ans;
int a[N],c[N],l[N],r[N];

int read() {
	int out=0,flag=1;
	char c=getchar();
	while(c<48||c>57) {
		if(c=='-') flag=-1;
		c=getchar();
	}
	while(c>=48&&c<=57) {
		out=out*10+c-48;
		c=getchar();
	}
	return out*flag;
}

bool cmp(int x,int y) {
	return c[x]<c[y];
}

void dfs(int now) {
	for(int i=l[now]; i<=r[now]; i++) {
		dfs(a[i]);
		c[now]++;
	}
	sort(a+l[now],a+r[now]+1,cmp);
	for(int i=l[now]; i<=r[now]; ++i) {
		if(c[now]+c[a[i]]-1<=m) {
			c[now]+=c[a[i]]-1;
			ans++;
		} else
			break;
	}
}

int main() {
	//scanf("%d%d",&n,&m);
	n=read(),m=read();
	for(int i=1; i<=n; i++)
		c[i]=read();
	for(int i=1,x; i<=n; i++) {
		x=read();
		l[i]=knt+1;
		while(x--)
			a[++knt]=read()+1;
		/*{
			knt++;
			scanf("%d",&a[knt]);
			a[knt]++;
		}*/
		r[i]=knt;
	}
	dfs(1);
	printf("%d\n",ans);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11800801.html