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
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <utility> #include <map> #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 300000 + 5;
int n, m, a[maxn], dep[maxn], fa[maxn]; vector<PII> cam[maxn]; map<int,ll> g[maxn];
int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { cam[i].clear(); g[i].clear(); } for (int i = 2; i <= n; i++) { scanf("%d", fa + i); dep[i] = dep[fa[i]] + 1; } ll ans = 0; for (int i = 1; i <= n; i++) { scanf("%d", a + i); ans += a[i]; } for (int i = 1, x, k, c; i <= m; i++) { scanf("%d%d%d", &x, &k, &c); cam[x].push_back({k, c}); } for (int i = n; i >= 1; i--) { g[i][dep[i]] += a[i]; for (auto& x: cam[i]) { int flow = x.second; int mxd = dep[i] + x.first; while (flow) { auto it = g[i].upper_bound(mxd); if (it == g[i].begin()) break; it--; ll tot = min(1ll * flow, it->second); flow -= tot; ans -= tot; it->second -= tot; if (it->second == 0) g[i].erase(it->first); } } if (i > 1) { int f = fa[i]; if ((int)g[i].size() > (int)g[f].size()) swap(g[i], g[f]); for (auto& x: g[i]) { if (x.second) g[f][x.first] += x.second; } } } printf("%lld\n", ans); } return 0; }
|