题目链接:Click here

Solution:

看起来不太能dp,则考虑树上贪心

题目要求一个点必须先染父亲才能染自己,就给了我们启示,贪心的合并点

我们定义一个点的权重为这个点的价值和/点数,然后贪心的选择权重最大的点加入到答案,合并到他的父亲

值得注意的是加入到答案的过程为加上这个点的价值和\(\times\)父亲节点的点数,因为这代表在他之前染色的点数

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,root;
struct node{
int s,v,f;
double w;
}tree[1001];
int find_max(){
int m;double maxn=0;
for(int i=1;i<=n;i++)
if(i!=root&&tree[i].w>maxn)
maxn=tree[i].w,m=i;
return m;
}
int main(){
while(1){
scanf("%d%d",&n,&root);
if(n==0&&root==0)break;
int ans=0;
for(int i=1;i<=n;i++){
scanf("%d",&tree[i].v);
tree[i].s=1;tree[i].w=double(tree[i].v);
ans+=tree[i].v;
}
for(int i=1;i<n;i++){
int a,b;scanf("%d%d",&a,&b);
tree[b].f=a;
}
for(int i=1;i<n;i++){
int now=find_max();
int fa=tree[now].f;
ans+=tree[now].v*tree[fa].s;
tree[now].w=0;
for(int j=1;j<=n;j++)
if(tree[j].f==now)tree[j].f=fa;
tree[fa].s+=tree[now].s;
tree[fa].v+=tree[now].v;
tree[fa].w=double(tree[fa].v)/tree[fa].s;
}printf("%d\n",ans);
}
return 0;
}

Color a Tree的更多相关文章

  1. POJ 2054 Color a Tree

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

  2. Color a Tree[HDU1055]

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

  3. Color a Tree HDU - 6241

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

  4. 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 ...

  5. Color a Tree & 排列

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

  6. 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 ...

  7. HDU - 6241 :Color a Tree(不错的二分)

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

  8. hdu 4603 Color the Tree

    这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...

  9. HDU 1055 - Color a Tree

    一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...

随机推荐

  1. java_实现一个类只能声明一个对象

    public class StaticDemo { public int a=10; private static StaticDemo s= new StaticDemo(); private St ...

  2. 洛谷 P1268 树的重量 题解

    题面 目的:求出树的各边长度 条件:每个节点之间最短路.整个图中不存在负边 我们可以每一次把一个点加入树内,求出这个点和已经构建好的树的边的长度: 这个长度抽象理解一下就是(dis[i][j]+dis ...

  3. 使用 vscode将本地项目上传到github、从github克隆项目以及删除github上的某个文件夹

    安装Git后,可以看到windows环境下有两个命令输入窗口Git CMD 和Git Bash Git GUI是可视化图形界面 Git中的Bash是基于CMD的,在CMD的基础上增添一些新的命令与功能 ...

  4. Redis利用Pipeline加速查询速度的方法

    1. RTT Redis 是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下 Redis 客户端执行一条命令分为如下四个过程: 发送命令 命令排队 命令执行 返回结果 客户 ...

  5. 第五章 表表达式 T-SQL语言基础

    表表达式 Microsoft SQL Server支持4种类型的表表达式:派生表(derived table),公用表表达式(CTE,common table expression),视图,以及内联表 ...

  6. springboot页面模板thymeleaf的简单用法

    thymeleaf基础语法: 变量输出与字符串操作: ​ th:text 表示在页面输出值 ​ th:value 表示将一个值放入input标签的value中 判断字符串是否为空: ​ thymele ...

  7. java读取blob全身乱码

    一.BLOB操作 .入库 ()JDBC方式 //通过JDBC获得数据库连接 Class.forName("oracle.jdbc.driver.OracleDriver"); Co ...

  8. Centos7:JDK1.8环境配置

    1.将压缩包解压缩 tar -zxvf jdk-8u181-linux-x64.tar.gz; 2.配置环境变量 环境变量地址:/etc/profile #set java environment J ...

  9. Slimvoice能代替JavaScript?

    对于Slimvoice(https://slimvoice.co/),我想反对JavaScript的炒作,并对整个应用程序进行服务器端渲染.您可能会说:“用户必须在使用应用程序时重新加载每个页面,这必 ...

  10. 批处理 使用默认浏览器 打开html文件

    @echo offfor /f "tokens=3,4" %%a in ('"reg query HKEY_CLASSES_ROOT\http\shell\open\co ...