神题。这题是巨毒瘤...

自己写真可谓是:

排空驭气奔如电,上天入地求之遍

上穷碧落下黄泉,两处茫茫皆不见

由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价。

那么我们就能想出一个错误的贪心:每次从能选的中选出最大的。

下面我们来构造反例:


root

↙     ↘

1         2

100


贪心:2 + 2 + 300 = 304

实际:1 + 200 + 6 = 207

那么我们该如何处理呢?

完全搞不倒啊!看题解看题解

得出如下结论:最大的节点一定是紧随着父节点染色的。

下一步:没有下一步....?????

我们要把这两个节点合并!

等效权值为平均数!

很遗恨的是,我只会证明两个点时权值是平均数。更多的还有待考证...

不过做题就是要靠想象!这就是正确的!

所以我们在代码实现的难题上继续败北......

我的想法是每个节点记录所有子节点以及所有合并过来的节点的顺序。用两个vector来实现。

然后那个堆是最困扰我的地方,实现起来困难重重。

看一看标程:cnm!直接n²暴力!

1000 * 1000 ......我觉得还行

然后我还发现了一件事:它没有记录次序!合并的同时更新ans!

惊为天人!

代码要简洁高效。这一点在寒假时我就有体会,现在又有这种感觉。

 /**
poj 2054
*/
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = ;
const double eps = 1e-; struct Node {
int tot, n, fa;
bool del;
double val;
}node[N]; int n, R; inline int find() {
double maxval = -;
int ans = ;
for(int i = ; i <= n; i++) {
if(!node[i].del && i != R && node[i].val - maxval > eps) {
maxval = node[i].val;
ans = i;
}
}
return ans;
} inline int getfa(int k) {
while(node[k].del) {
k = node[k].fa;
}
return k;
} int main() {
scanf("%d%d", &n, &R);
while(n || R) {
LL ans = ;
node[R].fa = ;
for(int i = ; i <= n; i++) {
scanf("%d", &node[i].tot);
node[i].val = (double)node[i].tot;
node[i].n = ;
node[i].del = ;
ans += node[i].tot;
}
for(int i = ; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
node[y].fa = x;
}
//printf("\n");
for(int i = ; i < n; i++) {
int s = find();
int f = getfa(node[s].fa);
//printf("%d %d\n", s, f);
ans += node[f].n * node[s].tot;
node[f].n += node[s].n;
node[f].tot += node[s].tot;
node[f].val = (double)(node[f].tot) / node[f].n;
//printf("%f\n", node[f].val);
node[s].del = ;
}
printf("%I64d\n", ans);
scanf("%d%d", &n, &R);
}
return ;
}

AC代码

poj2054 Color a Tree的更多相关文章

  1. [POJ2054]Color a Tree (并查集+贪心)

    POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...

  2. Poj2054 color a tree && [HNOI/AHOI2018]排列

    https://zybuluo.com/ysner/note/1120723 题面 原题 某省选强化题 大致意思是给你一颗树,选父亲后才能选儿子. 每个点对答案的贡献为你在第几次选这个点 × 该点权值 ...

  3. $Poj2054\ Color\ a\ Tree\ $ 贪心

    $poj$ $Description$ 一颗树有 $n$ 个节点,这些节点被标号为:$1,2,3…n,$每个节点 $i$ 都有一个权值 $A[i]$. 现在要把这棵树的节点全部染色,染色的规则是: 根 ...

  4. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  5. Color a Tree[HDU1055]

    Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. Color a Tree HDU - 6241

    /* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...

  7. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  8. Color a Tree & 排列

    Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父 ...

  9. hdu 6241 Color a Tree 2017 CCPC 哈理工站 L

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

随机推荐

  1. spring后置处理器BeanPostProcessor

    BeanPostProcessor的作用是在调用初始化方法的前后添加一些逻辑,这里初始化方法是指在配置文件中配置init-method,或者实现了InitializingBean接口的afterPro ...

  2. hive数学函数

    round 四舍五入 ceil向上 取整 floor向下取整 hive >  select floor(45.8); ok 45

  3. tensorflow实现基于LSTM的文本分类方法

    tensorflow实现基于LSTM的文本分类方法 作者:u010223750 引言 学习一段时间的tensor flow之后,想找个项目试试手,然后想起了之前在看Theano教程中的一个文本分类的实 ...

  4. windos安装maven

    1.下载好maven压缩包,并解压到相应位置,本次安装在D: 2.配置环境变量 MAVEN_HOME=D:\apache-maven-3.0.5 path=%MAVEN_HOME% 3.生成maven ...

  5. shell expr用法

    expr 计算整数变量值 使用方法如下: linux-zpycfm:/home/test/shell # s=+ -bash: +: command not found linux-zpycfm:/h ...

  6. delphi 中出现dataset not in edit or insert mode的问题

    self.ADOQuery2.Edit;self.ADOQuery2.First;while not self.ADOQuery2.Eof dobeginself.ADOQuery2.FieldByN ...

  7. SpringBoot之修改单个文件后立刻生效

    问题: 在使用SpringBoot进行开发时,如果修改了某个文件比如前端页面html,不能立刻起效. 解决: 在idea中打开修改后的文件,使用快捷键Ctrl+Shift+F9 进行重新编译,然后刷新 ...

  8. mybatis-spring-1.2.2.jar下载地址

    http://www.java2s.com/Code/Jar/m/Downloadmybatisspring120jar.htm

  9. Uncaught SyntaxError: Unexpected token export

    开发过程中遇到这个错误,虽然不影响使用,但是每次浏览器控制台都会有错误输出,看起来十分不舒服,故翻阅资料发现是因为浏览器虽然支持了es6,但是不支持es6的Module直接使用,需要在script标签 ...

  10. Luogu5205 【模板】多项式开根(NTT+多项式求逆)

    https://www.cnblogs.com/HocRiser/p/8207295.html 安利! 写NTT把i<<=1写成了i<<=2,又调了一年.发现我的日常就是数组开 ...