题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则,此博有很好的解释:http://www.cnblogs.com/rainydays/p/3271277.html 在此引用其中几段话: 试想,如果没有父节点排在节点之前的限制,那么这个题目非常简单,只需要将结点按照权值从大到小排列即可.加上了这个限制之后,如果权值最大的那个节点一旦满足了条件(父节点…
$ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节点染色前它的父亲节点也必须被染色,这就有了很多的后效性. 暂时没有办法贪心,我们就只能再找找性质.博主首先想到的是从叶子节点考虑,叶子节点里权值最小的一定最后染色.但是经过自我hack,这个结论也是错的.举个栗子:5-1-1-1-8,这条链上如果以左边第一个1为根,先染5会更优. 叶子节点我们拿他没…
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the "root" of the tree, and there is a unique path from the root to each of the other nodes. Bob intends to colo…
贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6647   Accepted: 2249 Description Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is single…
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int father[1010]; int next[1010];//当前集合的下个元素(包含i) int pre[1010];//当前集合的上个元素(包含i) int num[1010];//num[i]当前集合储存的点的个数(包含i) int vis[1010]; int sum[1010];//当前…
$poj$ $Description$ 一颗树有 $n$ 个节点,这些节点被标号为:$1,2,3…n,$每个节点 $i$ 都有一个权值 $A[i]$. 现在要把这棵树的节点全部染色,染色的规则是: 根节点R可以随时被染色;对于其他节点,在被染色之前它的父亲节点必须已经染上了色. 每次染色的代价为$T*A[i]$,其中$T$代表当前是第几次染色. 求把这棵树染色的最小总代价. $Sol$ 贪心:树中权值最大的点,一定会在它的父结点染色后立即染色 可以这样想,假设没有树的约束,只是一个序列,那么显然…
[题目链接] http://poj.org/problem?id=2054 [算法] 贪心 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include…
神题.这题是巨毒瘤... 自己写真可谓是: 排空驭气奔如电,上天入地求之遍 上穷碧落下黄泉,两处茫茫皆不见 由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价. 那么我们就能想出一个错误的贪心:每次从能选的中选出最大的. 下面我们来构造反例: root ↙     ↘ 1         2 ↓ 100 贪心:2 + 2 + 300 = 304 实际:1 + 200 + 6 = 207 那么我们该如何处理呢? 完全搞不倒啊!看题解看题解 得出如下结论:最大的节点一定是紧随着父节点染色的…
Color a Tree 题目链接 好不可做?可以尝试一下DP贪心网络流.DP 似乎没法做,网络流也不太行,所以试一下贪心. 考虑全局中最大权值的那个点,如果它没父亲,那么一定会先选它:否则,选完它父亲就一定先选它.于是我们可以把它缩成一个点. 但是我们并不知道缩点后的权值是多少,这样就没法继续缩下去.我们考虑一对被缩点的父子 \(x,y\),以及全局中的另一个点 \(a\),什么时候会先选 \(a\),什么时候先选 \(x,y\).如果先选 \(x,y\),那么就有 \(x + 2y + 3a…
Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 828    Accepted Submission(s): 272 Problem DescriptionBob is very interested in the data structure of a tree. A tree is a directed gr…