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

POJ终于修好啦
题意
和UVA1205是同一题,在洛谷上是紫题
有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每个节点的一个权值。(当前时间是染完这个节点的时间) 染色还有另一个约束条件,要染一个点必须要先染好其父节点,所以第一个染的点是根节点. 求最小开销。
思路
这道题显然对于 每一个可选的子节点选最重的 的贪心思路是错误的
就有点类似动态规划了 不过不DP也是可以贪心出来的。但是这个策略比较麻烦。大致思路就是父节点和子节点经过一些操作合并,合并之后贪心就是没问题的了。
不过我一开始自己的思路就是类似并查集+贪心的,不是合并(虽然差不多),就是全局贪心,把所有的点的权值排序,然后最大到最小的所有点慢慢并查集搜回去,直到所有点被处理过,也应该是可以的吧?但是时间复杂度肯定不行,于是就看李煜东的代码了(颓)。
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; #define N 1005 int n, r;
double c[N];
int nxt[N], la[N], num[N],fa[N];
double d[N],tot[N];
bool vis[N]; void color_a_tree()
{
for (int i = ; i <= n; i++)
{
scanf("%lf", &c[i]);
nxt[i] = i; la[i] = i; num[i] = ; tot[i] = c[i];
}
memcpy(d, c, sizeof(d));
for (int i = , a, b; i < n; i++)scanf("%d%d", &a, &b), fa[b] = a;
memset(vis, , sizeof(vis));
for (int i = ; i < n; i++)
{
int p;
double k = ;
for(int j=;j<=n;j++)
if (j != r && !vis[j] && c[j] > k)
{
k = c[j];
p = j;
}
int f = fa[p];
while (vis[f]) fa[p] = f = fa[f];//getfather
nxt[la[f]] = p;
la[f] = la[p];
num[f] += num[p];
tot[f] += tot[p];
c[f] = tot[f] / num[f];
vis[p] = ;
}
int ans = ;
for (int i = ; i <= n; i++)
{
ans += i * d[r];
r = nxt[r];
}
printf("%d\n", ans);
} int main()
{
while (scanf("%d%d", &n, &r) == && n && r) color_a_tree();
return ;
}
[POJ2054]Color a Tree (并查集+贪心)的更多相关文章
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
随机推荐
- 更换gcc工具链
title: 更换gcc工具链 date: 2019/1/16 19:27:51 toc: true --- 更换gcc工具链 下载后解压到一个临时目录先看看文件结构 mkdir tmp tar xj ...
- CMDB服务器管理系统【s5day90】:获取今日未采集主机列表
1.目录结构 1.服务器端 2.客户端 2.具体代码如下 1.数据库增加两个字段 class Server(models.Model): """ 服务器信息 " ...
- C++回顾day03---<多态>
一:错误理解下的多态 #include <iostream> using namespace std; class Parent { public: Parent() { cout < ...
- centos7防火墙设置
前言 CentOS7 与之前版本在防火墙配置上不同,防火墙从iptables变成了firewalld Centos7默认安装了firewalld,如果没有安装的话,可以使用yum命令进行安装 yum ...
- CentOS安装VLC
For EL7: rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm rpm - ...
- secureCRT免密码登陆Linux
转自:http://blog.csdn.net/wangquannetwork/article/details/46062675 1.实现原理: 通过CRT生成的密钥对,把公钥上传到Linux服务器指 ...
- 412 6个题 BOM and DOM 定义计数器 网页跳转 网页前进后退
AM BOM-JavaScript: 提供一系列对象哟用于和浏览器窗口交互,对象主要有 window.document.location.navigator.screen 统称浏览器对象模型(Brow ...
- LeetCode第十一题-可以装最多水的容器
Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...
- python加密
""#line:4 __all__ =[]#line:6 class OO0O0O000O0O0O000 :#line:8 ""#line:9 def __in ...
- tarjan 题目汇总(含解析)
下面容许我偷个懒,洛谷上写过的blog我就不来再抄一遍了 洛谷P3436 [[POI2006]PRO-Professor Szu](别称:作死的老教授) 洛谷P4306 [[JSOI2010]连通数] ...