BZOJ 2809: [Apio2012]dispatching

考虑枚举对于节点i作为领导 一定是把他的儿子们权值最小的几个作为工作忍者
用左偏树 大根堆维护这个关系 每次把最大的删掉 直到和<=S 按从叶子到根的顺序 每次把儿子的左偏树合并即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <vector>
#include <functional>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <set>
using namespace std;
typedef long long ll;

const ll MHmaxn = 100005;

struct MHnode
{
  ll l, r, dis, v;
};

struct MergeableHeap  //big first
{
  MHnode t[MHmaxn];
  ll tot;
 
  ll merge(ll a, ll b)
  {
    if (!a) return b;
    if (!b) return a;
    if (t[a].v < t[b].v) swap(a, b);
    t[a].r = merge(t[a].r, b);
    if (t[t[a].l].dis < t[t[a].r].dis) swap(t[a].l, t[a].r);
    t[a].dis = t[t[a].r].dis + 1;
    return a;
  }
 
  ll make_node(ll v)
  {
    ++tot;
    t[tot].l = t[tot].r = t[tot].dis = 0;
    t[tot].v = v;
    return tot;
  }
 
  ll pop(ll root)
  {
    return merge(t[root].l, t[root].r);
  }
 
  ll top(ll root)
  {
    return t[root].v;
  }
}mh;

struct line
{
  ll next, to;
}li[MHmaxn];

struct poll
{
  ll a, b, c;
  poll(ll x, ll y, ll z) : a(x), b(y), c(z) {}
};

ll be[MHmaxn], l, n, m, e[MHmaxn], c[MHmaxn], ans;

void makeline(ll fr, ll to)
{
  ++l;
  li[l].next = be[fr];
  be[fr] = l;
  li[l].to = to;
}

poll dfs(ll now)
{
  ll no = mh.make_node(c[now]), sum = c[now], tot = 1;
  for (ll i = be[now]; i; i = li[i].next)
  {
    ll to = li[i].to;
    poll t = dfs(to);
    no = mh.merge(no, t.a);
    sum += t.b;
    tot += t.c;
  }
  while (sum > m)
  {
    sum -= mh.top(no);
    no = mh.pop(no);
    --tot;
  }
  ans = max(ans, e[now] * tot);
  return poll(no, sum, tot);
}

int main()
{
  scanf("%lld%lld", &n, &m);
  ll root;
  for (ll i = 1; i <= n; ++i)
  {
    ll b;
    scanf("%lld%lld%lld", &b, &c[i], &e[i]);
    if (!b) root = i;
    else makeline(b, i);
  }
  dfs(root);
  printf("%lld", ans);
}

发表评论