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& ...
随机推荐
- MySQL数据库管理
好记性不如烂笔头 1.MySQL启动基本原理 /etc/init.d/mysqld 是一个shell启动脚本,启动后会调用mysqld_safe脚本,最后调用的是mysqld主程序启动mysql. 单 ...
- python有哪些比较隐藏的知识点?
一.复用小整数以及小字符串 在Python语言中,设计者在优化Python语言时,为了提高Python的运行效率,所以就将一些小整数保存在系统表,没有释放回收(正常情况下,当Python中一个对象没有 ...
- 50.RocketMQ (quickstart)
要多给下属表功,绝不能抢功. 1.订阅消息 /** * Copyright (C) 2010-2013 Alibaba Group Holding Limited * * Licensed under ...
- C#-WebForm-LinQ-条件精确查询、高级查询
前台界面,并在后台绑定数据 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ca ...
- 【面向对象】【prototype&&__proto__&&实例化对象三者之间的关系】
1.构造函数 a.什么是构造函数? 解释:通过关键字new 创建的函数叫做构造函数 作用:用来创建一个对象 废话少说直接上代码,首先我们还是创建一个构造函数人类 然后我们在创建两个实例,一个凡尘 一个 ...
- [转] 如何在 CentOS7 中使用阿里云的yum源
[From] https://www.cnblogs.com/lpbottle/p/7875400.html 1. 备份原来的yum源 mv /etc/yum.repos.d/CentOS-Base. ...
- mvn pom
使用本地指定目录下jar包 <dependency> <groupId>com.myself.abc</groupId> <artifactId>sam ...
- jmap查看内存使用情况与生成heapdump--转
http://www.webspherechina.net/home/space.php?uid=191&do=blog&id=7572 如果想分析自己的JAVA Applicatio ...
- 数据库sqlite3在linux中的使用
在linux下我们首先要获取root权限 当然也可是使用 sudo命令 接着让我们来安装sqlite3吧!博主当然是已经安装好了! 别急,的确你是安装好了sqlite3但是有一点必须要记住,你还没有安 ...
- vue做的简单购物车
<code><!DOCTYPE html><html><head lang="en"> <meta charset=" ...