Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define IN freopen("in.txt", "r", stdin);
- #define OUT freopen("out.txt", "w", stdout);
- #define CLOCK printf("time: %lld ms\n", (long long)clock() * 1000 / CLOCKS_PER_SEC);
- #define LL long long int
- #define MX 500001
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define max4(a, b, c, d) max(a, max3(b, c, d))
- #define min4(a, b, c, d) min(a, min3(b, c, d))
- #define EPS 1e-9
- #define MOD 1000000007
- #define PI 2.0 * acos(0.0)
- #define PII pair <int, int>
- #define VI vector <int>
- #define VVI vector <VI>
- vector <PII> g[MX];
- int joy[MX], par[MX], cst[MX], root, vis[MX], ind[MX];
- set <int> s[MX];
- int ans[MX];
- void dfs(int u) {
- vis[u] = 1;
- int cur = 0;
- for (auto p:g[u]) {
- int v = p.first;
- dfs(v);
- if (s[ind[u]].size() >= s[ind[v]].size()) {
- // cout << v << " to(1) " << u << endl; //ThisIsForDebuggingPurposes
- for (auto i:s[ind[v]]) {
- // cout << "i = " << i << endl; //ThisIsForDebuggingPurposes
- s[ind[u]].insert(i + p.second);
- }
- // cout << "u = " << u << endl; //ThisIsForDebuggingPurposes
- // cout << "set of u : ";
- // for (auto i:s[ind[u]]) {
- // cout << i << " ";
- // }
- // cout << endl;
- // cur = p.second;
- // ind[v] = ind[u];
- }
- else {
- // cout << u << " to(2) " << v << endl; //ThisIsForDebuggingPurposes
- for (auto i:s[ind[u]]) {
- // cout << "i = " << i << endl; //ThisIsForDebuggingPurposes
- s[ind[v]].insert(i - p.second + cur);
- }
- cur = p.second;
- ind[u] = ind[v];
- // cout << "v = " << v << endl; //ThisIsForDebuggingPurposes
- // cout << "set of v : ";
- // for (auto i:s[ind[v]]) {
- // cout << i << " ";
- // }
- // cout << endl;
- }
- // cout << "ind[" << u << "] = " << ind[u] << endl; //ThisIsForDebuggingPurposes
- // cout << "ind[" << v << "] = " << ind[v] << endl; //ThisIsForDebuggingPurposes
- }
- // cout << "s[ind[" << u << "]] = " << s[ind[u]].size() << endl; //ThisIsForDebuggingPurposes
- ans[u] = (s[ind[u]].size());
- }
- int main() {
- // IN OUT //ThisIsForDebuggingPurposes
- int n;
- scanf("%d", &n);
- for (int i = 1; i <= n; i++) {
- scanf("%d", &joy[i]);
- ind[i] = i;
- s[i].insert(joy[i]);
- }
- for (int i = 1; i <= n; i++) {
- scanf("%d", &par[i]);
- }
- for (int i = 1; i <= n; i++) {
- scanf("%d", &cst[i]);
- }
- for (int i = 1; i <= n; i++) {
- if (par[i] == 0) {
- root = i;
- }
- else {
- g[ par[i] ].push_back({i, cst[i]});
- }
- }
- dfs(root);
- for (int i = 1; i <= n; i++) {
- cout << ans[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement