首页 > 其他分享 >Educational Codeforces Round 168 (Rated for Div. 2) D题

Educational Codeforces Round 168 (Rated for Div. 2) D题

时间:2024-08-18 10:54:14浏览次数:10  
标签:Educational Rated int mn Codeforces dfs fa 权值 节点

文章目录

题目来源

D. Maximize the Root

题意

给定一棵n个点的数,根节点为1,每个点都有权值 a i a_i ai​
可以执行无限次操作:选择一个节点,将其子节点全部 − 1 -1 −1,自己的权值 + 1 +1 +1 (必须有子节点)
权值不能为负数,求根节点最大的权值

思路

考点:搜索+贪心

我们想让根节点的权值尽可能大,那么需要将除根节点外的最小权值尽可能变大
那么我们可以考虑dfs回溯的思想(或者用拓扑排序也可以)

从树的叶节点(树的末端)从后往前搜索
令mn为当前节点x的子节点的最小权值,那么当前节点的权值有2种可能:

  • a x < m n a_x<mn ax​<mn 那么可以将 a x a_x ax​不断+1,将mn不断-1,这样可以使最小权值变大,直到操作为 a x = m n a_x=mn ax​=mn ,那么它最后的权值就为 ( a x + m n ) / 2 (a_x+mn)/2 (ax​+mn)/2,向下取整
  • a x ≥ m n a_x≥mn ax​≥mn ,那么它可取到的最大权值就为 m n mn mn

当回溯到根节点1时,加上mn即可

code

const int N=1e6+5;
int a[N],b[N],fa[N];
vector<int> e[N];
void dfs(int x){
	int mn=1e10;
	for(auto y : e[x]){
		if(y==fa[x]) continue;//找到叶节点
		dfs(y);
		mn=min(mn,b[y]);//算出最小权值
	}
	if(mn==1e10){
		b[x]=a[x];
		return ;
	}
	if(x==1){
		a[1]+=mn;
		return ;
	}
	if(a[x]<mn) b[x]=(a[x]+mn)/2;
	else b[x]=mn;
}
void solve(){
	int n;cin >> n;
	for(int i=1;i<=n;++i){
		cin >> a[i];
		e[i].clear();
		b[i]=0; 
	}
	for(int i=2;i<=n;++i){
		cin >> fa[i];
		e[fa[i]].push_back(i);
		e[i].push_back(fa[i]);
	}
	dfs(1);
	cout << a[1] << endl;
	return ;
}

标签:Educational,Rated,int,mn,Codeforces,dfs,fa,权值,节点
From: https://blog.csdn.net/wh233z/article/details/141294979

相关文章

  • Codeforces Round 966 (Div. 3) (A~F)
    文章目录写在前面A-PrimaryTask思路codeB.SeatinginaBus思路codeC-NumericStringTemplate思路codeD-RightLeftWrong思路codeE-PhotoshootforGorillas思路codeF-ColorRowsandColumns思路codeCodeforcesRound966(Div.3)写在前面赛时写的......
  • Is it rated?
    题目大意给出序列p和实数k遍历p每次更新r为可以放弃m个位置,求最后r的max性质因为0.1<=k<=1所以r的系数最大为0.9p<=1e5所以设0.9^k*1e5=1e-9所以大约310次后最开始的r的影响是1e-9,误差范围1e-9,所以310次后最开始的r就没用了思路我们只需要考虑最靠后的310次rat......
  • Codeforces 169 Div2
    AClosestPoint由题意可得三个及以上的点无法插入新的点,只有两个点时可以插入但当两个点间隔为1时同样无法插入先判断,后输出就行#include<bits/stdc++.h>usingnamespacestd;constintN=50;intt,n;inta[N];intmain(){ cin>>t; while(t--){ cin>>n; for(i......
  • 自创CodeForcesHelperv1.0,解决CF太卡和跳题问题,代码持续更新!
    文章目录前言一、方法二、源代码三、使用方法四、效果展示总结前言对于OIers来说,Codeforces是一个很好的外国OJ。洛谷上确实收录了大部分CF的题,但是最近由于Codeforces的cloudflare加强了,所以洛谷的爬虫已经无法正确爬取提交记录的数据了,详见link。我......
  • Codeforces Round 894 (Div. 3) D
    题目:E.KolyaandMovieTheatre题目链接:https://codeforces.com/contest/1862/problem/E思路:主要用优先队列存放大于0的元素,然后除了第一个数据后的每m个数据就可以存放到记录数组里面,最后遍历找价值最大的点击查看代码#include<bits/stdc++.h>#defineintlonglongusing......
  • codeforces ECR169
    codeforcesECR169A#include<bits/stdc++.h>usingnamespacestd;constintmaxn=50;inta[maxn];voidsolve(){intn;cin>>n;for(inti=1;i<=n;i++)cin>>a[i];if(n==2){if((a[2]-a[1])!=1){......
  • CodeForces 1575F Finding Expected Value
    洛谷传送门CF传送门考虑单个序列如何求答案。考虑鞅与停时定理。定义一个局面的势能为\(\sum\limits_{i=0}^{K-1}f(b_i)\),其中\(f(x)\)是一个关于\(x\)的函数,\(b_i\)为\(i\)的出现次数。那么我们要构造\(f(x)\),使得每次操作,局面势能期望减少\(1\),那么期望步数......
  • Codeforces 232 B Table 题解 [ 蓝 ] [ 分组背包 ] [ 组合数学 ] [ 循环节 ]
    Codeforces232BTable。蒟蒻模拟赛上场切的一道蓝,非常难以置信我竟然能做蓝题。这题的数据范围初看还是比较坑的,\(10^{18}\)的值域很容易让人往矩阵加速那方面想。实际上在列出转移方程式后,我们发现状态是二维的,无法使用矩阵加速(或者说这样做很麻烦)。思路首先观察到每个边长......
  • Codeforces Round 966 (Div. 3) VP
    A.PrimaryTask枚举所有情况即可voidsolve(){ strings; cin>>s; if(s.substr(0,2)!="10"||s.size()<3||s[2]=='0'||(s.size()<4&&s[2]<'2')){ cout<<"NO\n"; } else{......
  • [CodeForces] F. Color Rows and Columns
    ProblemLink Basedoninitialobservation,itseemsthatgreedilypickthesmallestrow/columnlengthworks.Butthelastexampletestcaseoutputs35whilegreedygives36.  Howyoushouldgofromthere:1.checkifyourgreedyimplementationisco......