题意

题目链接

给出一棵树,每个节点有权值,选出\(k\)个联通块,最大化

\[\frac{\sum_{i \in S} a_i}{k}
\]

Sol

结论:选出的\(k\)个联通块的大小是一样的且都等于最大联通块的大小

证明:因为我们是在保证分数最大的情况下才去最大化\(k\),一个很经典的结论是单独选择一个权值最大的联通块得到的分数一定是最大的,然后我们这时我们才去考虑最大化\(k\)

那么思路就很清晰了,先一遍dfs dp出最大联通块,然后再一遍dfs从下往上删就行了

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 3e5 + 10, INF = 1e18;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, a[MAXN], mx[MAXN], ans = -INF, num;
#define siz(v) ((int)v.size())
vector<int> v[MAXN];
void dfs(int x, int fa) {
mx[x] = a[x];
for(int i = 0; i < siz(v[x]); i++) {
int to = v[x][i];
if(to == fa) continue;
dfs(to, x);
mx[x] = max(mx[x], mx[x] + mx[to]);
}
ans = max(ans, mx[x]);
}
void dfs2(int x, int fa) {
mx[x] = a[x];
for(int i = 0; i < siz(v[x]); i++) {
int to = v[x][i];
if(to == fa) continue;
dfs2(to, x);
mx[x] = max(mx[x], mx[x] + mx[to]);
}
if(mx[x] == ans) num++, mx[x] = 0;
}
signed main() {
#ifndef ONLINE_JUDGE
//freopen("a.in", "r", stdin);freopen("a.out", "w", stdout);
#endif
N = read();
for(int i = 1; i <= N; i++) a[i] = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dfs(1, 0);
//printf("%I64d\n", ans);
memset(mx, 0, sizeof(mx));
dfs2(1, 0);
cout << ans * num << " " << num;
return 0;
}

cfE. Ehab and a component choosing problem(贪心)的更多相关文章

  1. Codeforces 1088E Ehab and a component choosing problem

    Ehab and a component choosing problem 如果有多个连接件那么这几个连接件一定是一样大的, 所以我们先找到值最大的连通块这个肯定是分数的答案. dp[ i ]表示对于 ...

  2. 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 题意: 给出一个 ...

  3. 【数学/贪心/DP】【CF1088E】 Ehab and a component choosing problem

    Description 给定一棵 \(n\) 个节点的树,点有点权 \(a_u\),可能为负.现在请你在树上找出 \(k~(1~\leq~k~\leq~n)\) 个不相交集合,使得每个集合中的每对点都 ...

  4. cf1088E Ehab and a component choosing problem (树形dp)

    题意(考试时看错了对着样例wa了好久..):从树上选k个连通块,使得权值的平均值最大的基础上,选的块数最多 如果不考虑块数最多的限制,肯定是只选一个权值最大的块是最好的 然后只要看这个权值最大的块有多 ...

  5. Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学

    题意:给出树 求最大的sigma(a)/k k是选取的联通快个数   联通快不相交 思路: 这题和1个序列求最大的连续a 的平均值  这里先要满足最大平均值  而首先要满足最大  也就是一个数的时候可 ...

  6. Codeforces Round #525 E - Ehab and a component choosing problem

    题目大意: 在一棵树中 选出k个联通块 使得 这k个联通块的点权总和 / k 最大 并且这k个联通块不相互覆盖(即一个点只能属于一个联通块) 如果有多种方案,找到k最大的那种 给定n 有n个点 给定n ...

  7. CF D. Ehab and the Expected XOR Problem 贪心+位运算

    题中只有两个条件:任意区间异或值不等于0或m. 如果只考虑区间异或值不等于 0,则任意两个前缀异或值不能相等. 而除了不能相等之外,还需保证不能出现任意两个前缀异或值不等于m. 即 $xor[i]$^ ...

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

  9. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

随机推荐

  1. numpy 模块常用方法

    Numpy是科学计算库,是一个强大的N维数组对象ndarray,是广播功能函数.其整合C/C++.fortran代码的工具 ,更是Scipy.Pandas等的基础 .ndim :维度 .shape : ...

  2. linux C API连接并查询mysql5.7.9

    开发环境: ubuntu16.04 mysql5.7.9 原生C API VIM 配置远程连接 配置mysql允许远程连接的方法默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/e ...

  3. allure报告定制(pytest+jenkins)

    环境及安装可查看 pytest+jenkins安装+allure导出报告 要让allure报告更漂亮,更直观,需要在脚本中写入allure特性 一开始allure调用step().story().fe ...

  4. UVA_11020 Efficient Solutions 【平衡二叉搜索树set用法】

    一.题面 UVA11020 二.分析 最近脑子有点不好使吧,这题还想了很久. 对于给定的两个值要满足题面中的条件,那么我们可以把这两个值转化到平面中的坐标去理解. 首先,需要考虑的是维护的所有点其实是 ...

  5. 使用nginx+uwsgi+Django环境部署

    环境准备 Python点这里 nginx点这里 uwsgi点这里

  6. Flask基本知识

    @app.route('/')def hello_world(): return 'Hello World!' #route动态Route,支持字符串.整数.浮点数,/user/<int:id& ...

  7. Visual Studio 跨平台開發實戰(4) - Xamarin Android 基本控制項介紹 (转帖)

    前言 不同於iOS, Xamarin 在Visual Studio中針對Android, 可以直接設計使用者介面. 在本篇教學文章中, 筆者會針對Android的專案目錄結構以及基本控制項進行介紹, ...

  8. (转)搞个这样的APP要多久?心酸啊。

    这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...

  9. javascript中Function与Object

    1. 先来一段代码: console.log(Function); // function Function() { [native code] } console.log(Object); // f ...

  10. 四大组件之Service

    1 通过startService 1.1 与调用者相对独立,没有返回,用于播放音乐,下载文件 1.2 如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行.下次 ...