[HEOI2015]兔子与樱花

嘟嘟嘟

自底向上贪心,优先删除每一个儿子中代价最小的一个。

正确性我也不是十分明白,画图的时候发现贪心删边都对,于是试一试竟然AC了……

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(' ')
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 const int maxn = 2e6 + 5;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ' ';
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27     if(last == '-') ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) x = -x, putchar('-');
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + '0');
35 }
36 
37 int n, m;
38 
39 struct Edge
40 {
41     int nxt, to;
42 }e[maxn << 1];
43 int head[maxn], ecnt = -1;
44 void addEdge(int x, int y)
45 {
46     e[++ecnt] = (Edge){head[x], y};
47     head[x] = ecnt;
48 }
49 
50 int t[maxn], c[maxn], ans = 0;
51 void dfs(int now)
52 {
53     int cnt = 0;
54     for(int i = head[now]; i != -1; i = e[i].nxt) dfs(e[i].to);
55     for(int i = head[now]; i != -1; i = e[i].nxt) t[++cnt] = c[e[i].to];
56     sort(t + 1, t + cnt + 1);
57     for(int i = 1; i <= cnt; ++i)
58     {
59         if(c[now] + t[i] - 1 > m) break;
60         c[now] += t[i] - 1, ans++;
61     }
62 }
63 
64 int main()
65 {
66     Mem(head, -1);
67       n = read(); m = read();
68       for(int i = 0; i < n; ++i) c[i] = read();
69       for(int i = 0; i < n; ++i)
70     {
71         int k = read(); c[i] += k;
72           for(int j = 1; j <= k; ++j)
73         {
74               int x = read();
75               addEdge(i, x); 
76           }
77     }
78       dfs(0);
79       write(ans), enter;
80       return 0;
81 }
View Code
原文地址:https://www.cnblogs.com/mrclr/p/9862267.html