【题目链接】

http://poj.org/problem?id=2054

【算法】

贪心

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1010 struct info
{
double w,size;
} a[MAXN]; int i,n,r,ans,pos,u,v,j;
int fa[MAXN];
bool vis[MAXN];
double mx;
vector< int > son[MAXN]; int main()
{ while (scanf("%d%d",&n,&r) && n && r)
{
ans = ;
memset(vis,false,sizeof(vis));
for (i = ; i <= n; i++) son[i].clear();
for (i = ; i <= n; i++)
{
scanf("%lf",&a[i].w);
ans += (int)a[i].w;
a[i].size = ;
}
for (i = ; i < n; i++)
{
scanf("%d%d",&u,&v);
fa[v] = u;
son[u].push_back(v);
}
vis[r] = true;
for (i = ; i < n; i++)
{
mx = ;
for (j = ; j <= n; j++)
{
if (!vis[j] && double(a[j].w / a[j].size) > mx)
{
mx = double(a[j].w / a[j].size);
pos = j;
}
}
ans += a[fa[pos]].size * a[pos].w;
vis[pos] = true;
a[fa[pos]].w += a[pos].w;
a[fa[pos]].size += a[pos].size;
for (j = ; j < son[pos].size(); j++)
{
fa[son[pos][j]] = fa[pos];
son[fa[pos]].push_back(son[pos][j]);
}
}
printf("%d\n",ans);
} return ; }

【POJ 2054】 Color a Tree的更多相关文章

  1. 【POJ 2154】 Color (置换、burnside引理)

    Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...

  2. 【POJ 2154】Color

    http://poj.org/problem?id=2154 还是先套上Burnside引理:\[\begin{aligned} ans & =\sum_{i=1}^n n^{(i,n)-1} ...

  3. 【POJ 1741】Tree

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description ...

  4. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  5. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  6. 【POJ 2750】 Potted Flower(线段树套dp)

    [POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   ...

  7. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  8. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  9. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

随机推荐

  1. vue基础---Class 与 Style 绑定

    [一]绑定HTML Class (1)对象语法 ①普通绑定class <div id="area" v-bind:class="className"> ...

  2. docker 1-->docker compose 转载

    转自:http://www.ityouknow.com/docker/2018/03/22/docker-compose.html Docker-Compose 是 Docker 的一种编排服务,是一 ...

  3. 微服务网关从零搭建——(三)Ocelot网关 + identity4

    增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...

  4. Vue.js 安装

    注意:vue.js 不支持 IE8 及其以下版本,学习前请保证你的浏览器兼容 ECMAScript 5,可访问 http://caniuse.com/#feat=es5 查看支持 ECMAScript ...

  5. [Luogu] P4626 一道水题 II

    ---恢复内容开始--- 题目描述 一天,szb 在上学的路上遇到了灰太狼. 灰太狼:帮我们做出这道题就放了你. szb:什么题? 灰太狼:求一个能被 [1,n] 内所有数整除的最小数字,并对 100 ...

  6. FATE HDU - 2159

    解法 完全背包但是又有别的条件(忍耐值为体力经验是价值) ①首先杀怪是有上限s的,所以需要记录杀怪的数量并且if时候还需要加上条件 ②最后还得遍历一下从小到大遍历当前dp范围内是不是已经有够经验的那么 ...

  7. X shell 6下载安装和简单使用

    ①前言:昨天已经上线了的智能家居项目出现了一个BUG,需要重新写个html发布到服务器上,由于公司大佬都在忙别的项目,时间比较紧张,这种小事就落到了我这个小喽啰身上.其实,写个html我还是可以接受的 ...

  8. Python学习:ModuleNotFoundError: No module named 'pygal.i18n' 的解决方法

    最近在学<Python编程:从入门到实践>,16.2小结中 from pygal.i18n import COUNTRIES 获取两个字母的国别码,我用的pygal的版本是2.4.0(终端 ...

  9. Linux学习总结(18)——Linux使用init命令关机、重启、切换模式

    reboot可能是每个用过Linux的人都知道的命令,但有一个命令"init"才是命令中的精英. 最近有个同事学习安装了CentOS,明明安装的是带桌面的系统,但是启动后进入了命令 ...

  10. [luoguP2045] 方格取数加强版(最小费用最大流)

    传送门 水题 ——代码 #include <queue> #include <cstdio> #include <cstring> #include <ios ...