CF1466-D. 13th Labour of Heracles

题意:

给出一个由\(n\)个点构成的树,每个点都有一个权值。现在你可以用\(k,k\subset\)\([1, n]\)个颜色来给这棵树上的边涂色(这\(k\)种颜色不一定都要用上)。对于每种颜色都有一个权重,权值是这样定义的:

将除了当前颜色\(col_i\)其他颜色的边删掉,剩余的边构成了一个个联通分量。对于任意一个联通分量我们设它的权重是\(w_i\),那么\(w_i\)就等于该联通分量中所有点的权值之和,则对于当前颜色,它的权值就是\(w_{col_i}=max\{w_1, w_2,\cdots,w_n\}\)

需要注意的是,这个联通分量是通过删边得到的,所以不会出现联通分量中只有一个点的情况。对于一个空的联通分量,我们认为它的权重为0。

现在又定义对于每个\(k\)也有一个权值,它等于它包含的所有颜色的权值之和,即\(w_k=\sum_{i=1}^kw_{col_i}\).

现在让你求出对于每个\(k\),它的权值\(w_k\)的最大值是多少。


思路:

首先非常显然的一个道理,我们不能涂完颜色后让一个或多个颜色被分割成多个联通分量然后取\(max\),这样无论如何都不可能比单一的联通分量的权值大。

我们可以试着模拟从涂一个颜色到涂两个颜色的过程, 变化的就是让其中一个点从只被计算一次变成被计算两次,对于从两个颜色到三个,三个到四个...都可以依次推广得到相同的结论。所以这里利用贪心的思想优先让权值大的点先被计算多次,再依次减少。

对于每一个点,他能够被计算的最多的次数就是它的度\((degree)\)。当\(k=1\)的时候,它的最大值只有一个就是每个点的权值之和。这时候每个边都会被计算一次,所有的边度都减去一,对于剩下的所有度还大于1的点,我们就按照上面说到的贪心方法进行计算就可以得到最终结果。


AC代码:

这段代码我借鉴了一些大佬的数据处理方法。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> typedef long long ll; const int maxn = 200005; ll w[maxn], a[maxn << 1];
int deg[maxn], tot = 0; int main() {
int cases, n;
scanf("%d", &cases);
while(cases--) {
tot = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
deg[i + 1] = 0;
scanf("%lld", &w[i + 1]);
}
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d %d", &u, &v);
deg[u]++; deg[v]++;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < deg[i]; j++) {
a[tot++] = w[i];
}
ans += w[i];
}
std::sort(a, a + tot, std::greater<int>());
for (int i = 0; i < tot; i++) {
printf("%lld ", ans);
ans += a[i];
}
printf("%lld\n", ans);
} return 0;
}

CF1466-D. 13th Labour of Heracles的更多相关文章

  1. 0001-Weekly Meeting on 13th and 20th March, 2015

    13th March, 2015 (1) Nearest Neighbors Using Compact Sparse Coding  ->appearing in ICML2014       ...

  2. The 13th tip of DB Query Analyzer, powerful processing EXCEL file

    The 13thtip of DB Query Analyzer, powerful processing EXCEL file MA Genfeng (Guangdong UnitollServic ...

  3. October 13th 2016 Week 42nd Thursday

    If the world seems cold to you, kindle fires to warm it. 若世界以寒相待,请点燃火堆以温暖相报. Kindle fires to warm th ...

  4. August 13th 2016 Week 33rd Saturday

    What makes life dreary is the want of motive. 没有目标与动力,生活便会郁闷无光. Without dreams and hope, there will ...

  5. The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)

      前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解   ZOJ 3946 Highway ...

  6. July 13th, Week 29th Wednesday, 2016

    Travel imparts new vigor to the mind. 旅行能给思想带来新的活力. Travel can give us opportunities to experience m ...

  7. February 13th, 2018 Week 7th Tuesday

    You are your greatest asset. 你就是你自己最大的资本. For most of us, there are few things that we can count on ...

  8. January 13th, 2018 Week 02nd Saturday

    Anyone who has no spiritual aspirations is an idiot. 任何没有精神追求的人都是愚昧无知的人. Today I went to a bookshop ...

  9. November 13th, 2017 Week 46th Monday

    Don't undermine your worth by comparing yourself with others. 别拿自己和他人比较,这只会降低你原有的价值. Honestly, I don ...

随机推荐

  1. Trollcave-suid提权

    一 扫描端口 扫描开放端口:nmap -sV -sC -p- 192.168.0.149 -oA trollcave-allports 扫描敏感目录:gobuster dir -u http://19 ...

  2. bash shell数组使用总结

    本文为原创博文,转发请注明原创链接:https://www.cnblogs.com/dingbj/p/10090583.html  数组的概念就不多说了,大家都懂! shell数组分为索引数组和关联数 ...

  3. k8s集群中遇到etcd集群故障的排查思路

    一次在k8s集群中创建实例发现etcd集群状态出现连接失败状况,导致创建实例失败.于是排查了一下原因. 问题来源 下面是etcd集群健康状态: 1 2 3 4 5 6 7 8 9 10 11 [roo ...

  4. 手机QQ空间自动点赞登录

    学以致用~使用 Appium 模拟人类操控手机行为 V2.0在手机上运行,目前实现以下功能: 1.小黑屋模式,一分钟内给好友发100条消息然后进了好友的小黑屋 2.定时发消息提醒对象多喝热水~ 3.对 ...

  5. 阿里云镜像仓库镜像迁移至私有Harbor

    下载镜像同步工具 wget https://goodrain-delivery.oss-cn-hangzhou.aliyuncs.com/boe/image-syncer && chm ...

  6. error: Failed dependencies: rpm安装包失败报错依赖包

    error: Failed dependencies: mysql-community-release conflicts with (installed) mysql57-community-rel ...

  7. thinkphp如何实现伪静态

    去掉 URL 中的 index.php ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好.但 ThinkPHP 提供了各种机制来定制需要的 URL 格式,配合 ...

  8. vue-cli快速创建项目,可视化创建

    之前学习了交互式创建,发现过程无聊,而且不方便,后面又学习了图形可视化创建,下面进行分享 1.打开cmd 2.输入vue ui,输入后会出现如下 C:\Users\12235>vue ui St ...

  9. : cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANs

    : cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANs

  10. mysqldump 内存消耗

    MySQL :: MySQL 8.0 Reference Manual :: 4.5.4 mysqldump - A Database Backup Program https://dev.mysql ...