poj2054 Color a Tree
神题。这题是巨毒瘤...
自己写真可谓是:
排空驭气奔如电,上天入地求之遍
上穷碧落下黄泉,两处茫茫皆不见
由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价。
那么我们就能想出一个错误的贪心:每次从能选的中选出最大的。
下面我们来构造反例:
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的更多相关文章
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- Poj2054 color a tree && [HNOI/AHOI2018]排列
https://zybuluo.com/ysner/note/1120723 题面 原题 某省选强化题 大致意思是给你一颗树,选父亲后才能选儿子. 每个点对答案的贡献为你在第几次选这个点 × 该点权值 ...
- $Poj2054\ Color\ a\ Tree\ $ 贪心
$poj$ $Description$ 一颗树有 $n$ 个节点,这些节点被标号为:$1,2,3…n,$每个节点 $i$ 都有一个权值 $A[i]$. 现在要把这棵树的节点全部染色,染色的规则是: 根 ...
- POJ 2054 Color a Tree
贪心.... Color a Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: ...
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Color a Tree HDU - 6241
/* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...
- 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 ...
- Color a Tree & 排列
Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父 ...
- 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 ...
随机推荐
- 【学亮IT手记】jQuery each()函数用法实例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- drf 之序列化组件
序列化 把Python中对象转换为json格式字符串 反序列化 把json格式转为为Python对象. 用orm查回来的数据都是都是一个一个的对象, 但是前端要的是json格式字符串. 序列化两大功能 ...
- python中的 list (列表)append()方法 与extend()方法的用法 和 区别
参考: https://www.cnblogs.com/xuchunlin/p/5479119.html
- vue & iview
vue & iview ui components https://codepen.io/webgeeker/pen/EJmQxQ https://www.iviewui.com/docs/g ...
- python设计模式第十九天【职责链模式】
1.应用场景 (1)将一个任务拆分为具有顺序的多个部分,每个类完成相应的部分,并且顺序执行 (2)软件窗口的消息传播 (3)SERVLET容积的过滤器Filter的实现 2.代码实现 #!/usr/b ...
- JUC虚假唤醒(六)
为什么条件锁会产生虚假唤醒现象(spurious wakeup)? 在不同的语言,甚至不同的操作系统上,条件锁都会产生虚假唤醒现象.所有语言的条件锁库都推荐用户把wait()放进循环里: whil ...
- 【C/C++】递归算法
所谓递归——函数的递归调用.c语言的这种特性给程序设计带来许多方便.尤其是接触数据结构时,会发现递归的出现频率非常之高,也行之有效~下面是笔者在接触递归这个东西时的一些个人总结和体会: 1.直接或间接 ...
- fastjson 操作
1.String 转 bean String addition = ...; CoffeeFormula formula = JSON.parseObject(addition, new TypeRe ...
- Luogu4726 【模板】多项式指数函数(NTT+多项式求逆)
https://www.cnblogs.com/HocRiser/p/8207295.html 安利! #include<iostream> #include<cstdio> ...
- ajax 提交数组 泛型集合(嵌套集合)
直接上代码 后台接口: A类型中嵌套了 List<B> B类型中嵌套了 List<C> [HttpPost] public string Post(A a) { return ...