cfE. Ehab and a component choosing problem(贪心)
题意
给出一棵树,每个节点有权值,选出\(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(贪心)的更多相关文章
- Codeforces 1088E Ehab and a component choosing problem
Ehab and a component choosing problem 如果有多个连接件那么这几个连接件一定是一样大的, 所以我们先找到值最大的连通块这个肯定是分数的答案. dp[ i ]表示对于 ...
- 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 题意: 给出一个 ...
- 【数学/贪心/DP】【CF1088E】 Ehab and a component choosing problem
Description 给定一棵 \(n\) 个节点的树,点有点权 \(a_u\),可能为负.现在请你在树上找出 \(k~(1~\leq~k~\leq~n)\) 个不相交集合,使得每个集合中的每对点都 ...
- cf1088E Ehab and a component choosing problem (树形dp)
题意(考试时看错了对着样例wa了好久..):从树上选k个连通块,使得权值的平均值最大的基础上,选的块数最多 如果不考虑块数最多的限制,肯定是只选一个权值最大的块是最好的 然后只要看这个权值最大的块有多 ...
- Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学
题意:给出树 求最大的sigma(a)/k k是选取的联通快个数 联通快不相交 思路: 这题和1个序列求最大的连续a 的平均值 这里先要满足最大平均值 而首先要满足最大 也就是一个数的时候可 ...
- Codeforces Round #525 E - Ehab and a component choosing problem
题目大意: 在一棵树中 选出k个联通块 使得 这k个联通块的点权总和 / k 最大 并且这k个联通块不相互覆盖(即一个点只能属于一个联通块) 如果有多种方案,找到k最大的那种 给定n 有n个点 给定n ...
- CF D. Ehab and the Expected XOR Problem 贪心+位运算
题中只有两个条件:任意区间异或值不等于0或m. 如果只考虑区间异或值不等于 0,则任意两个前缀异或值不能相等. 而除了不能相等之外,还需保证不能出现任意两个前缀异或值不等于m. 即 $xor[i]$^ ...
- 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 ...
- [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& ...
随机推荐
- C++实现二叉树的相应操作
1. 二叉树的遍历:先序(递归.非递归),中序(递归.非递归),后序(递归.非递归). #include <iostream> #include <string> #inclu ...
- 总结day2 ---- while循环的简单使用, 格式化输出.运算符.以及编码的应用
内容提要 一 : while 循环 while 的基本语句操作 如何终止循环 二 :格式化输出 三 :运算符号 四 :编码初识别 一 : while 循环 1 >>>>whi ...
- Learn to See in the Dark(论文阅读笔记)
最近做项目看了一篇论文<Learn to See in the Dark>下面是一些论文笔记 概括: 这篇论文主要介绍的是在低光照的环境下用两个标准的FCN网络,通过控制变量法来对比不同的 ...
- 快速沃尔什变换 FWT 学习笔记【多项式】
〇.前言 之前看到异或就担心是 FWT,然后才开始想别的. 这次学了 FWT 以后,以后判断应该就很快了吧? 参考资料 FWT 详解 知识点 by neither_nor 集训队论文 2015 集合幂 ...
- pyserial timeout=1 || timeout=0.01
昨天在做串口通信时候发现,串口参数(timeout=1 || timeout=0.01)对通信的读数据竟然影响很大,代码如下: self.ser = serial.Serial(port=serial ...
- mono for android生成APK出现错误fatal error in gc 解决方案
laxknight 标签: mono for android,fatal error in gc mono for android生成APK出现这个错误fatal error in gc collec ...
- 【软件】chrome设置默认字体
安装stylish插件 新建样式,加入代码 * { font-family: "Microsoft YaHei", "微软雅黑" !important; }
- C# 文件读写系列二
读取文件原则上非常简单,但它不是通过FileInfo和DirectoryInfo来完成的,关于FileInfo和DirectoryInfo请参考C# 文件操作系列一,在.Net Framework4. ...
- Tomcat *的下载(绿色版和安装版都适用)
不多说,直接干货! 1.先下载tomcat,到http://tomcat.apache.org/ 2.注意:下载可以下载zip格式或exe格式的,其中zip格式的只要解压缩再配置下环境变量就可以使用了 ...
- Javac词法分析
参考:<深入分析Java Web>技术内幕 许令波 词法分析过程涉及到的主要类及相关的继承关系如下: 词法分析的接口为Lexer,默认实现类为Scanner,Scanner会逐个读取Jav ...