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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include <bits/stdc++.h> using namespace std;
int n, a, b; const int N = 262144; long long C[N]; vector<int> g[N]; long long dep[N]; long long sum[N]; long long dp[N];
void dfs(int u) { sum[u] = dep[u] * C[u]; for (auto v : g[u]) { dep[v] = dep[u] + a; dfs(v); sum[u] += sum[v]; } }
template<class T> void cmin(T &x, const T&y) { if (x > y) x = y; } namespace noya { #define int long long int rt[N], ls[N << 5], rs[N << 5], id[N << 5], cnt = 0; struct Line { int k, b; inline int val(int x) { return k * x + b; } } s[N << 5]; int line_cnt = 0; int add_line(int k, int b) { line_cnt++; s[line_cnt] = {k, b}; return line_cnt; } int add_line(Line a, int b) { line_cnt++; s[line_cnt] = {a.k, a.b + b}; return line_cnt; } void ins(int &p, int l, int r, int num) { if(!p) { p = ++cnt; id[p] = num; return; } int mid = l + r >> 1, &x = num, &y = id[p]; int midx = s[x].val(mid), midy = s[y].val(mid); if(midx < midy) { x ^= y ^= x ^= y; } int lx = s[x].val(l), ly = s[y].val(l); int rx = s[x].val(r), ry = s[y].val(r); if(lx >= ly && rx >= ry) return; if(lx < ly) ins(ls[p], l, mid, num); else ins(rs[p], mid + 1, r, num); } int qry(int p, int l, int r, int pos) { if(!p) return 1e18; if(l == r) { return s[id[p]].val(pos); } int mid = l + r >> 1, ans = s[id[p]].val(pos); if(pos <= mid) { cmin(ans, qry(ls[p], l, mid, pos)); } else { cmin(ans, qry(rs[p], mid + 1, r, pos)); } return ans; } #undef int }
using namespace noya; vector<long long> lines[N]; long long add_tag[N];
const int L = 2e9; void dfs2(int u) { for (auto v : g[u]) { dfs2(v); } auto merge = [&](int u, int v, long long tag) { if (lines[u].size() > lines[v].size()) { long long add = add_tag[v] + tag - add_tag[u]; for (auto id : lines[v]) { int cur = add_line(s[id], add); ins(rt[u], 0, L, cur); lines[u].push_back(cur); } } else { long long add = add_tag[u] - tag - add_tag[v]; for (auto id : lines[u]) { int cur = add_line(s[id], add); ins(rt[v], 0, L, cur); lines[v].push_back(cur); } swap(rt[u], rt[v]); swap(lines[u], lines[v]); add_tag[u] = tag + add_tag[v]; } }; long long cur = 0; for (auto v : g[u]) cur += dp[v]; { int id = add_line(C[u], cur); lines[u].push_back(id); ins(rt[u], 0, L, id); }
long long prefix = 0; for (auto v : g[u]) { cur -= dp[v]; merge(u, v, cur + prefix + C[u] * dep[u]); prefix += sum[v]; }
dp[u] = min(sum[u], qry(rt[u], 0, L, dep[u] + b) + add_tag[u]); }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> a >> b;
for (int i = 1; i <= n; i++) { int c, k; cin >> c >> k; C[i] = c; for (int j = 0; j < k; j++) { int x; cin >> x; g[i].push_back(x); } }
dfs(1); memset(dp, 0x3f, sizeof dp); dfs2(1); cout << dp[1] << "\n"; return 0; }
|