Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem
题目链接:https://codeforces.com/contest/1088/problem/E
题意:
给出一个数,找出k个连通块,使得连通块里面点的和除以k最大。当选择不同数量的连通块有相同的最大值时,要求输出k最大的情况。
题解:
由于这个不等式average(x1,x2,x3...xn)<=max(x1,x2,...xn)成立(当且仅当x1=x2=...xn时,等号成立),而题目所求正好是连通块里面点和的平均值。
我们要让average(x1,x2,x3...xn)最大,一是尽量满足等号成立的条件,二是尽量让连通块里面的权和最大。
由于题目中要求k尽量最大。
所以我们可以进行两次dfs,第一次找出最大的连通块,第二次看看有多少个连通块等于之前找到的最大值,并且更新k的值以及所有点的权值和。
求最大连通块的时候用到最大连续子段和的技巧,负数就舍去,当儿子为正数才更新值。
代码如下:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#define INF 9999999999
using namespace std; const int N = 3e5+ ;
typedef long long ll;
int a[N],head[N],vis[N];
int n,tot,k;
struct Edge{
int u,v,next;
}e[N<<];
ll dp[N];
ll ans=-INF;
void adde(int u,int v){
e[++tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot;
e[++tot].u=v;e[tot].v=u;e[tot].next=head[v];head[v]=tot;
}
void dfs(int node,int f){
dp[node]=a[node];
vis[node]=;
for(int i=head[node];i!=-;i=e[i].next){
int v=e[i].v;
if(!vis[v]){
vis[v]=;
dfs(v,f);
if(dp[v]>) dp[node]+=dp[v];
}
}
if(!f) ans=max(ans,dp[node]);
else{
if(dp[node]==ans){
k++;
dp[node]=;
}
}
}
int main(){
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=,u,v;i<=n-;i++){
scanf("%d%d",&u,&v);
adde(u,v);
}
dfs(,);
memset(vis,,sizeof(vis));
dfs(,);
cout<<ans*k<<" "<<k<<endl;
return ;
}
Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem的更多相关文章
- Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学
题意:给出树 求最大的sigma(a)/k k是选取的联通快个数 联通快不相交 思路: 这题和1个序列求最大的连续a 的平均值 这里先要满足最大平均值 而首先要满足最大 也就是一个数的时候可 ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(待完成)
参考资料: [1]:https://blog.csdn.net/weixin_43790474/article/details/84815383 [2]:http://www.cnblogs.com/ ...
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(交互题 异或)
题目 题意: 0≤a,b<2^30, 最多猜62次. 交互题,题目设定好a,b的值,要你去猜.要你通过输入 c d : 如果 a^c < b^d ,会反馈 -1 : 如果 a^c = b^ ...
- Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula
F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task 数学 mod运算的性质
C. Ehab and a 2-operation task 数学 mod运算的性质 题意: 有两种对前缀的运算 1.对前缀每一个\(a +x\) 2.对前缀每一个\(a\mod(x)\) 其中x任选 ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task
传送门 https://www.cnblogs.com/violet-acmer/p/10068786.html 题意: 给定一个长度为 n 的数组a[ ],并且有两种操作: ①将前 i 个数全都加上 ...
随机推荐
- 列表排序之NB三人组附加一个希尔排序
NB三人组之 快速排序 def partition(li, left, right): tmp = li[left] while left < right: while left < ri ...
- jmeter 插件安装
1.下载Plugins Manager插件 打开下载插件地址:https://jmeter-plugins.org/ 2.将下载的plugins-manager.jar包复制到Jmeter安装目录,l ...
- Linux命令备忘录:quota显示磁盘已使用的空间与限制
quota命令用于显示用户或者工作组的磁盘配额信息.输出信息包括磁盘使用和配额限制. 语法 quota(选项)(参数) 选项 -g:列出群组的磁盘空间限制: -q:简明列表,只列出超过限制的部分: - ...
- u-boot.bin生成过程分析
ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...
- 通过SVI实现VLAN间通信
两个不同网段的计算机与三层交换机直连,通过SVI实现VLAN间通信vlan 1 //几个不同网段就创建几个VLANvlan 2 int f0/1 //划分VLANswitchport mode acc ...
- 新手学习ARM,对片内ram、SDRAM、NOR FLASH和NAND FLASH启动这几个概念的理解
片内的ram用来存储启动代码,在2440初始化sdram之前,代码就在片内ram中运行.片内ram装载的是norflash中的内容,即u-boot. uboot放在norflash里,nandflas ...
- .NET CORE LOG
.NET CORE LOG 合格的应用程序不仅要求运行的高效和计算的准确,稳定及可靠性也要得到满足,同事,系统的可维护性也相当重要.谈及到可维护性,就必须涉及到系统运行状态的监控和异常的快速定位与跟踪 ...
- 安装sql server
因为电脑中只有mysql数据库,所以昨天准备安装一个sql server.安装中出现了许多问题,首先第一遍的时候,安装组件中没有勾选管理工具这个选项,所以在最后的时候,文件夹中只有配置管理器,没有数据 ...
- fsync体会
看这个链接:http://www.postgresql.org/docs/9.1/static/runtime-config-wal.html 是这样说的: fsync (boolean) If th ...
- GreenMail邮件测试服务器
GreenMail邮件测试服务器 http://blog.csdn.net/jackiehff/article/details/8741988 这个目前没有需求,所以暂不研究