Codeforces Round #678 (Div. 2)—— D. Bandit in a City(图论)

Codeforces Round #678 (Div. 2)—— D. Bandit in a City

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

The city consists of n squares connected by n−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 ai citizens on the i-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 n — the number of squares in the city (2≤n≤2⋅105).

The second line contains n−1 integers p2,p3…pn meaning that there is a one-way road from the square pi to the square i (1≤pi<i).

The third line contains n integers a1,a2,…,an — the number of citizens on each square initially (0≤ai≤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.

题意: 有n个地点,有n-1条路把点联通全是单向路,1是根。有一个强盗会抓人,他回去人最多的一个叶子节点。每个点有一些人,他们可以延路往下走直到走不了。问最后所有的叶子节点中人最多的是几个人。

思路: 我们发现,对于每一个不是叶子节点的点来说,我们都需要尽量把所有的人平均分,才能保证人最多的叶子节点是最小值。有一种特殊情况就是可能有个子节点他所拥有的人比把所有根节点的人全部放到其他子节点中还要多,但是这种情况就被计算以这个点为根节点的情况覆盖了,所以我们最后只要计算每个点的子节点数量和整个子树的人数就行了,遍历一下每个节点,答案就是max(ans,sum[i]/zi[i]+(sum[i]%zi[i]!=0))。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6;
ll ren[maxn],fu[maxn];
ll head[maxn],nxt[maxn],cnt=0,to[maxn],ww[maxn];
int vis[maxn];
ll sum[maxn],zi[maxn];
void add(ll x,ll y,ll w)
{
	nxt[++cnt]=head[x],to[cnt]=y,ww[cnt]=w,head[x]=cnt;
}
void dfs(ll u)
{
	int flag=0;
	sum[u]=ren[u];
	for(int i=head[u];i;i=nxt[i])
	{
		ll v=to[i];
		dfs(v);
		flag=1;
		zi[u]+=zi[v];
		sum[u]+=sum[v];
	}
	if(flag==0)zi[u]=1;
}
int main(){
	ll n;
	scanf("%lld",&n);
	cnt=0;
	for(int i=2;i<=n;i++)
	{
		ll x;
		scanf("%lld",&x);
		add(x,i,0);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&ren[i]);
	}
	dfs(1);
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,sum[i]/zi[i]+(sum[i]%zi[i]!=0));
	}
	printf("%lld\n",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值