【POJ 2054】 Color a Tree
【题目链接】
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的更多相关文章
- 【POJ 2154】 Color (置换、burnside引理)
Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...
- 【POJ 2154】Color
http://poj.org/problem?id=2154 还是先套上Burnside引理:\[\begin{aligned} ans & =\sum_{i=1}^n n^{(i,n)-1} ...
- 【POJ 1741】Tree
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11570 Accepted: 3626 Description ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
随机推荐
- oracle文件 结构01
1.减少数据的冗余(例如使用id) 2.保证数据库一致性 关联表越多越慢 主机名 hosts 文件 ntp 时间同步
- BZOJ2657: [Zjoi2012]旅游(journey) (树形DP)
题意:一个三角划分的凸多边形 画一条对角线 穿过最多的三角形 题解:把每一个三角形看作一个点 如果某条边是两个三角形的公共边 那么就把这两个三角形连边 然后问题就转化为求树上的最长链了 就当求个直径就 ...
- 话说Form标签的target属性-----无刷新表单提交
国庆前(2013)无聊,就在铁道部的12306上“逛”了下下. PS:原来之所以叫12306,是因为其客服号码是12306,好吧,我很无知…… 首先是被“逛”的页面:票价查询. 之所以去逛,是因为一直 ...
- TWaver MONO Design中动画的导出与播放
使用过MONO编辑器的朋友都应该了解MONO中支持一些动画的设置,比如开门,开窗,弹出设备,旋转场景,镜头巡航等.但如何将这些动画应用到自己的场景中呢?比如我们在编辑器中给机柜定义了动画,怎样让做好的 ...
- Oracle创建用户、角色、授权、建表空间
oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...
- 常量、变量;基本数据类型;input()、if、while、break、continue
一.编译型语言和解释型语言区别:编译型:一次性将所有程序编译成二进制文件 缺点:开发效率低,不能跨平台 优点:运行速度快. 例如:C,C++等解释型:当程序执行时,一行一行的解释 优点:开发效率高,可 ...
- msdn的原版windows下载地址链接
http://msdn.itellyou.cn/ 所有版本的下载地址 进去点左边操作系统
- vue中路由
关于每次点击链接都要刷新页面的问题众所周知,开发单页应用就是因为那丝般顺滑的体验效果,如果每次点击都会刷新页面… 出现这个的原因是因为使用了window.location来跳转,只需要使用使用rout ...
- 高德地图将字符串地址转为经纬度的一个demo
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- 【04】JSONP 教程
JSONP 教程 Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不同的域 ...