mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:31:59 +00:00
D - Fake Plastic Trees
https://codeforces.com/contest/1694/submission/160924995
This commit is contained in:
parent
7010cdd6c6
commit
68041768d2
63
Codeforces/1694/D/D.cpp
Normal file
63
Codeforces/1694/D/D.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user