【题目链接】

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. CAD把控件图形变成二进制流输出(com接口VB语言)

    主要用到函数说明: _DMxDrawX::WriteBinStream 把控件图形变成二进制流输出,详细说明如下: 参数 说明 VARIANT* pVarBinArray 输出二进制数组.是个byte ...

  2. 梦想CAD控件安卓控件事件

    MxDrawActivity.commandEvent 命令调用事件. 参数 说明 int iCommand 命令ID,这个ID用户自已来取的,只要多个命令ID不重复就可以 代码实现如下: publi ...

  3. Mybatis学习总结一

    一.Mybatis架构  JAR包下载地址 1.  mybatis配置 SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息. mapper. ...

  4. java虚拟机(二)--类加载机制和双亲委派模型

    一.类的生命周期 加载(Loading).验证(Verification).准备(Preparation).解析(Resolution).初始化(Initialization).使用(Using).卸 ...

  5. crm01 Django-admin管理工具

    admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...

  6. 弹性分布式数据集(RDD)

    spark围绕弹性分布式数据集(RDD)的概念展开的,RDD是一个可以并行操作的容错集合. 创建RDD的方法: 1.并行化集合(并行化驱动程序中现有的集合) 调用SparkContext的parall ...

  7. 再读Android sqlite

    再读Android sqlite Android原生支持sqlite数据库操作,sqlite时轻量级关系型数据库,支持标准sql语句.Android对sqlite进行良好的接口封装来避免sql注入等安 ...

  8. <MyBatis>入门四 传入的参数处理

    1.单个参数 传入单个参数时,mapper文件中 #{}里可以写任意值 /** * 传入单个参数 */ Employee getEmpById(Integer id); <!--单个参数 #{} ...

  9. 每日命令:(1)ls

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  10. vue中axios设置

    //设置默认全局baseURL axios.defaults.baseURL=process.env.BASE_API; //设置默认全局携带浏览器cookie axios.defaults.with ...