0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 14:56:27 +00:00

D - Fake Plastic Trees

https://codeforces.com/contest/1694/submission/160924995
This commit is contained in:
Baoshuo Ren 2022-06-17 14:05:06 +08:00
parent 7010cdd6c6
commit 68041768d2
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

63
Codeforces/1694/D/D.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cstring>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e5 + 5;
int t, n, ans;
long long f[N];
std::vector<int> g[N];
std::pair<int, int> p[N];
void dfs(int u) {
for (int v : g[u]) {
dfs(v);
f[u] += f[v];
}
if (f[u] < p[u].first) {
ans++;
f[u] = p[u].second;
}
f[u] = std::min(f[u], (long long)p[u].second);
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t;
while (t--) {
memset(f, 0x00, sizeof(f));
ans = 0;
cin >> n;
for (int i = 2, x; i <= n; i++) {
cin >> x;
g[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> p[i].first >> p[i].second;
}
dfs(1);
for (int i = 1; i <= n; i++) {
g[i].clear();
}
cout << ans << endl;
}
return 0;
}