Bandit in a City

链接 : http://codeforces.com/problemset/problem/1436/D

思路见 : https://www.cnblogs.com/blacktion/p/13872413.html

代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 2e5 + 7;
int n;
int idx, e[N], ne[N], h[N];

struct node {
    ll w, cnt, ans;
}p[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

node dfs(int u) {
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        node t = dfs(j);
        p[u].w += t.w;
        p[u].cnt += t.cnt;
        p[u].ans = max(p[u].ans, t.ans);  
    }
    p[u].ans = max(p[u].ans,  (p[u].w + p[u].cnt - 1) / p[u].cnt);
    return p[u];
}

int main() {	
    IO;
    memset(h, -1, sizeof h);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) p[i].cnt = 1;
    for (int i = 2; i <= n; ++i) {
        int x;
        cin >> x;
        add(x, i);
        p[x].cnt = 0;
    }
    for (int i = 1; i <= n; ++i) {
        cin >> p[i].w;
        if (p[i].cnt == 1) p[i].ans = p[i].w;
    }
    cout << dfs(1).ans << endl;
    //for (int i = 1; i <= n; ++i) cout << p[i].w << " " << p[i].cnt << " " << p[i].ans << endl;
    return 0;
}

posted @ 2020-11-11 15:53  phr2000  阅读(59)  评论(0编辑  收藏  举报