题目大意
给出一棵有
n
n
n个结点的一棵树,
1
1
1好结点时根结点,第
i
i
i个结点上有
a
i
a_i
ai个人。
现在有一个坏人出现在根结点,开始来抓人:
每个结点的人先跑到与该结点相邻的点(不能跑向父亲结点),
然后坏人在跑到某一个相邻的结点。
当坏人走到叶子结点的时候停止,该叶子结点上的所有人被坏人抓获。
已知所有人与坏人都知道彼此的位置,他们都是聪明绝顶的,每一步都是最优策略。
问坏人抓到的人数最少能是多少。
时间限制
1s
数据范围
n ≤ 2 × 1 0 5 n\le2\times 10^5 n≤2×105
题解
如果认为这题是个博弈题的话,其实是很难模拟这个过程的。
很显然,最终每个叶子结点的人数约平均,被抓的人越少。因为那个坏人也是聪明绝顶的,只有有一个叶子结点的人数较多,那么他显然会去那个结点。
考虑一个结点
x
x
x,则
a
x
a_x
ax个人只能跑到它子树里的叶子结点。
不妨记答案为
a
n
s
ans
ans,那么每个叶子的最终人数就不能超过
a
n
s
ans
ans。
如果记
f
x
f_x
fx表示
x
x
x及其子树的
a
i
a_i
ai和,
g
x
g_x
gx表示
x
x
x及其子树中叶子节点的个数。
那么不难发现
a
n
s
×
g
x
≤
f
x
ans\times g_x\le f_x
ans×gx≤fx
因此就可以在
O
(
n
)
O(n)
O(n)的时间里求出答案。
Code
//#pragma GCC optimize (2)
//#pragma G++ optimize (2)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#define G getchar
#define ll long long
using namespace std;
int read()
{
char ch;
for(ch = G();(ch < '0' || ch > '9') && ch != '-';ch = G());
int n = 0 , w;
if (ch == '-')
{
w = -1;
ch = G();
} else w = 1;
for(;'0' <= ch && ch <= '9';ch = G())n = (n<<1)+(n<<3)+ch-48;
return n * w;
}
const int N = 200003;
int n , a[N] , nxt[N] , to[N] , lst[N] , tmp;
ll f[N] , g[N] , ans;
bool dfs(int x)
{
if (lst[x] == 0) g[x]++;
f[x] = a[x];
for (int i = lst[x] ; i ; i = nxt[i])
{
dfs(to[i]);
f[x] = f[x] + f[to[i]];
g[x] = g[x] + g[to[i]];
}
ll tmp = f[x] / g[x];
if (f[x] % g[x]) tmp ++;
ans = max (ans , tmp);
}
int main()
{
//freopen("c.in","r",stdin);
//freopen("2.txt","w",stdout);
n = read();
for (int i = 2 ; i <= n ; i++)
{
tmp = read();
nxt[i] = lst[tmp];
to[i] = i;
lst[tmp] = i;
}
for (int i = 1 ; i <= n ; i++)
a[i] = read();
ans = 0;
dfs(1);
printf("%lld\n", ans);
return 0;
}