【Codeforces 1436 D】Bandit in a City,贪心,DFS,多叉树


D. Bandit in a City
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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?

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).

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

1 1
3 1 2
1 1
3 1 3
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个点的树,每个点有点权,现在可以把点权向孩子分配。
+ 求当左右点权都分配到叶子时,哪种方案下叶子的最大权值是最小的。
+ 最理想的方案下,每一根节点的全部人(包含子节点)都平均的走向不同的路,这样最终的最大权值就是总权值/叶子节点个数。
+ 但是因为是单向路不能回头,所以可能某个子节点x的权值已经非常大了,无论怎么分配其他权值都不能完全平均,所以此时最大权值一定在这个x的子节点中诞生,一定往x走(同时此时父节点应不给x分配任何权值)。但是再考虑此时他再当前子树中肯定是拉低最大值的,所以直接统计全局最大值即可。
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 998244353;

LL a[maxn], sum[maxn], cnt[maxn], ans;
void dfs(int u){
	sum[u] = a[u];
	for(auto v:G[u]){
		cnt[u] += cnt[v];
		sum[u] += sum[v];
	if(!G[u].size())cnt[u] = 1;
	if(sum[u]%cnt[u]==0)ans = max(ans,sum[u]/cnt[u]);
	else ans = max(ans,sum[u]/cnt[u]+1);

int main(){
	int n;  cin>>n;
	for(int i = 2; i <= n; i++){
		int x;  cin>>x;
	for(int i = 1; i <= n; i++)cin>>a[i];
	return 0;

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页