Codeforces Round#525(Div.2)Ehab and a component choosing problem CodeForces - 1088E
题意:
有一个拥有 n 个节点的树,你要选 k 个联通块出来,使得这 k 个联通块中所有点的权值总和 sum 与联通块个数 k 的比值最大,多解时应使联通块的数量尽可能地多
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long int ll;
const int maxn =3e5+7;
const int maxm=10000;
const int mod =1e9+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;int n,a[maxn],cnt;
vector<int>edge[maxn];
ll maxx=-mod;
ll dfs(int u,int pre,bool flag)
{ll sum=a[u];for(int v:edge[u])if(v!=pre)sum+=max(0ll,dfs(v,u,flag));if(!flag)maxx=max(maxx,sum);else if(sum==maxx)cnt++,sum=0;return sum;
}int main()
{scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<n;i++){int u,v; scanf("%d%d",&u,&v);edge[u].push_back(v);edge[v].push_back(u);}dfs(1,1,false),dfs(1,1,true);printf("%lld %d\n",cnt*maxx,cnt);return 0;
}
Codeforces Round#525(Div.2)Ehab and a component choosing problem CodeForces - 1088E相关推荐
- Codeforces 1088E Ehab and a component choosing problem(树形DP)
Codeforces 1088E Ehab and a component choosing problem(树形DP) 题意 给一棵树,要求从中选一些联通分量,使得平均联通分量重量总和最大.如果有多 ...
- Codeforces - Ehab and a component choosing problem
题目链接:Codeforces - Ehab and a component choosing problem 显然 Σa / k,相当于取平均值,我们得到最大值肯定是k=1的时候. 求最大值树形dp ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))...
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- 1574D The Strongest Build (Educational Codeforces Round 114 (Rated for Div. 2))
题意 给定n个从小到大的数组,从每个数组中选出一个下标构成一个序列,但是有m种序列被ban了,要求选出的序列对应的数字和最大且没有被ban. 思路 因为有m个序列被ban了,那么最坏的情况可以假设为最 ...
- Codeforces Round 649 (Rated for Div. 2)D. Ehab s Last Corollary详细题解(图论+简单环)
树 边 : 树边: 树边:深度优先森林中的边.如果结点v是因对(u,v)的探索而首先被发现,则(u,v)是一条树边. 后 向 边 : 后向边: 后向边:后向边(u,v)是将结点u连接到其在深度优先树中 ...
- CodeForces - 1088E Ehab and a component choosing problem(树形dp)
题目链接:点击查看 题目大意:给出一棵树,每个顶点都有权值,在树上选出k个相互独立的连通块,使得其权值和的平均值最大的情况下选的块数最多 题目分析:这个题目中平均值的优先级大于块数,那么我们可以在树上 ...
- Educational Codeforces Round 104 (Rated for Div. 2)A~E解题报告
Educational Codeforces Round 104 (Rated for Div. 2) A. Arena \quad原题链接 http://codeforces.com/contest ...
- Educational Codeforces Round 62 (Rated for Div. 2)
layout: post title: Educational Codeforces Round 62 (Rated for Div. 2) author: "luowentaoaa&quo ...
- Educational Codeforces Round 108 (Rated for Div. 2)-B. The Cake Is a Lie-题解
目录 Educational Codeforces Round 108 (Rated for Div. 2)-B. The Cake Is a Lie Problem Description Inpu ...
最新文章
- 【渝粤教育】 国家开放大学2020年春季 1443卫生信息与文献检索 参考试题
- 解决vlc-android播放http视频退出问题
- 鸽主姓名查询成绩_鸽主姓名
- python面值组合_算法题 - 拼凑面额 - Python
- 全网首发:怎样制作CDKEY(4)-生成CDKEY
- 知乎万赞:熬夜整理出的软件测试面试题大全(2021最新)
- 中科大EPC抢预约考试脚本简单版
- 应用wms仓库管理系统,提高第三方物流企业竞争力
- 移动支付走入线下商家
- 运兴ETF:期权多空双向,策略多样优势大
- Oracle 常见错误总结及问题解决方法
- 车载计算机维修,车载电脑硬件故障及检修
- 菜单栏点击显示二级菜单_显示完整菜单
- TypeScript 入门教程
- javascript ipz127
- jsp中文件上床的enctype=multipart/form-data用法
- suse系统bond网卡并且设置vlanID
- 哪款计算机可以玩游戏,哪个云电脑配置较高?能用来玩游戏?
- matebookego win11 arm 系统安装安卓子系统
- 新型邮件营销骗局“骗QQ群”