Advertisement
Wazedrifat

raff.cpp

Jul 8th, 2020
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1.     #include <bits/stdc++.h>
  2.     using namespace std;
  3.  
  4.     #define IN freopen("in.txt", "r", stdin);
  5.     #define OUT freopen("out.txt", "w", stdout);
  6.     #define CLOCK printf("time: %lld ms\n", (long long)clock() * 1000 / CLOCKS_PER_SEC);
  7.     #define LL long long int
  8.     #define MX 500001
  9.     #define max3(a, b, c) max(a, max(b, c))
  10.     #define min3(a, b, c) min(a, min(b, c))
  11.     #define max4(a, b, c, d) max(a, max3(b, c, d))
  12.     #define min4(a, b, c, d) min(a, min3(b, c, d))
  13.     #define EPS 1e-9
  14.     #define MOD 1000000007
  15.     #define PI 2.0 * acos(0.0)
  16.     #define PII pair <int, int>
  17.     #define VI vector <int>
  18.     #define VVI vector <VI>
  19.  
  20.     vector <PII> g[MX];
  21.     int joy[MX], par[MX], cst[MX], root, vis[MX], ind[MX];
  22.     set <int> s[MX];
  23.     int ans[MX];
  24.  
  25.     void dfs(int u) {
  26.         vis[u] = 1;
  27.         int cur = 0;
  28.  
  29.         for (auto p:g[u]) {
  30.             int v = p.first;
  31.             dfs(v);
  32.            
  33.             if (s[ind[u]].size() >= s[ind[v]].size()) {
  34.                 // cout << v << " to(1) " << u << endl;     //ThisIsForDebuggingPurposes
  35.                 for (auto i:s[ind[v]]) {
  36.                     // cout << "i = " << i << endl;     //ThisIsForDebuggingPurposes
  37.                     s[ind[u]].insert(i + p.second);
  38.                 }
  39.                
  40.                 // cout << "u = " << u << endl;     //ThisIsForDebuggingPurposes
  41.                 // cout << "set of u : ";
  42.                 // for (auto i:s[ind[u]]) {
  43.                 //  cout << i << " ";
  44.                 // }
  45.                 // cout << endl;
  46.                 // cur = p.second;
  47.                 // ind[v] = ind[u];
  48.             }
  49.             else {
  50.                 // cout << u << " to(2) " << v << endl;     //ThisIsForDebuggingPurposes
  51.                 for (auto i:s[ind[u]]) {
  52.                     // cout << "i = " << i << endl;     //ThisIsForDebuggingPurposes
  53.                     s[ind[v]].insert(i - p.second + cur);
  54.                 }
  55.                 cur = p.second;
  56.                 ind[u] = ind[v];
  57.  
  58.                 // cout << "v = " << v << endl;     //ThisIsForDebuggingPurposes
  59.                 // cout << "set of v : ";
  60.                 // for (auto i:s[ind[v]]) {
  61.                 //  cout << i << " ";
  62.                 // }
  63.                 // cout << endl;
  64.             }
  65.  
  66.             // cout << "ind[" << u << "] = " << ind[u] << endl;     //ThisIsForDebuggingPurposes
  67.             // cout << "ind[" << v << "] = " << ind[v] << endl;     //ThisIsForDebuggingPurposes
  68.         }
  69.  
  70.         // cout << "s[ind[" << u << "]] = " << s[ind[u]].size() << endl;        //ThisIsForDebuggingPurposes
  71.  
  72.         ans[u] = (s[ind[u]].size());
  73.     }
  74.  
  75.  
  76.     int main() {
  77.         // IN OUT       //ThisIsForDebuggingPurposes
  78.        
  79.         int n;
  80.         scanf("%d", &n);
  81.        
  82.         for (int i = 1; i <= n; i++) {
  83.             scanf("%d", &joy[i]);
  84.             ind[i] = i;
  85.             s[i].insert(joy[i]);
  86.         }
  87.  
  88.         for (int i = 1; i <= n; i++) {
  89.             scanf("%d", &par[i]);
  90.         }
  91.         for (int i = 1; i <= n; i++) {
  92.             scanf("%d", &cst[i]);
  93.         }
  94.  
  95.         for (int i = 1; i <= n; i++) {
  96.             if (par[i] == 0) {
  97.                 root = i;
  98.             }
  99.             else {
  100.                 g[ par[i] ].push_back({i, cst[i]});
  101.             }
  102.         }
  103.  
  104.  
  105.         dfs(root);
  106.  
  107.         for (int i = 1; i <= n; i++) {
  108.             cout << ans[i] << endl;
  109.         }
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement