Codeforces1436 D. Bandit in a City(贪心,树形DP)

Bandits appeared in the city! One of them is trying to catch as many citizens as he can.

The city consists of 𝑛 squares connected by 𝑛−1 roads in such a way that it is possible to reach any square from any other square. The square number 1 is the main square.

After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.

At the moment when the bandit appeared on the main square there were 𝑎𝑖 citizens on the 𝑖-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.

The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?

Input
The first line contains a single integer 𝑛 — the number of squares in the city (2≤𝑛≤2⋅105).

The second line contains 𝑛−1 integers 𝑝2,𝑝3…𝑝𝑛 meaning that there is a one-way road from the square 𝑝𝑖 to the square 𝑖 (1≤𝑝𝑖<𝑖).

The third line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 — the number of citizens on each square initially (0≤𝑎𝑖≤109).

Output
Print a single integer — the number of citizens the bandit will catch if both sides act optimally.

Examples
inputCopy
3
1 1
3 1 2
outputCopy
3
inputCopy
3
1 1
3 1 3
outputCopy
4
Note
In the first example the citizens on the square 1 can split into two groups 2+1, so that the second and on the third squares will have 3 citizens each.

In the second example no matter how citizens act the bandit can catch at least 4 citizens.

题意:
你在根节点,每秒你走一个格子。你到了叶子就抓住叶子上所有人。
每个点都有一些人,每一秒钟每个人向下走一个点,最终只能走到叶子。
你和其他人轮流走,你希望抓到最多人,其他人希望抓到尽量少的人。

思路:
对于每个点上的人,要使得你抓到的尽可能少,那么就要使得每个叶子上的人分配的尽量平均。第一步就是将所有叶子加到不超过最大值,如果还有剩余的人,那就平均分配。

则定义 d p [ i ] dp[i] dp[i]代表第 i i i个节点出发的最大值, L e f t [ i ] Left[i] Left[i]为第 i i i个节点对应所有叶子加到最大值所需要的人数。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;
vector<int>G[maxn];
int a[maxn];
ll dp[maxn],Left[maxn],siz[maxn];

void DP(int x) {
    if(G[x].size() == 0) {
        dp[x] = a[x];
        siz[x] = 1;
        return ;
    }
    ll mx = 0;
    for(int i = 0;i < G[x].size();i++) {
        int v = G[x][i];
        DP(v);
        siz[x] += siz[v];
        mx = max(mx,dp[v]);
    }
    ll num = a[x];
    for(int i = 0;i < G[x].size();i++) {
        int v = G[x][i];
        num -= Left[v] + siz[v] * (mx - dp[v]);
    }
    if(num > 0) mx += (num + siz[x] - 1) / siz[x];
    for(int i = 0;i < G[x].size();i++) {
        int v = G[x][i];
        Left[x] += Left[v] + siz[v] * (mx - dp[v]);
    }
    Left[x] -= a[x];
    dp[x] = mx;
}

int main() {
    int n;scanf("%d",&n);
    for(int i = 2;i <= n;i++) {
        int x;scanf("%d",&x);
        G[x].push_back(i);
    }
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    
    DP(1);
//    for(int i = 1;i <= n;i++) {
//        printf("%lld %lld\n",dp[i],Left[i]);
//    }
//    printf("\n");
    printf("%lld\n",dp[1]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值