hdu1055
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<string>
#include<vector>
#define eps 1e-6
#define LL long long
#define LD long double
#define pi acos(-1.0)
#define inf 1<<30
using namespace std;
struct Node{
int idx,next;
}a[1005];
int cnt,n,root,c[1005],num[1005],pre[1005],start[1005];
bool visit[1005];
int addedge(int u,int v){
a[cnt].idx=v;
a[cnt].next=start[u];
return cnt++;
}
int find_root(){
double mmax=0;
int k;
for(int i=1;i<=n;i++)
if(!visit[i]&&(double)c[i]/num[i]>mmax&&i!=root){
mmax=(double)c[i]/num[i];
k=i;
}
return k;
}
void Union(int k,int p){
num[p]+=num[k];
c[p]+=c[k];
for(int i=start[k];i;i=a[i].next)
pre[a[i].idx]=p;
}
int slove(){
int ans=0;
for(int i=1;i<n;i++){
int k=find_root();
visit[k]=true;
int p=pre[k];
while(visit[p]) p=pre[p];
ans+=num[p]*c[k];
Union(k,p);
}
return ans+c[root];
}
int main(){
while(scanf("%d%d",&n,&root)!=EOF&&n+root){
memset(start,0,sizeof(start));
memset(visit,false,sizeof(visit));
for(int i=1;i<=n;i++){
scanf("%d",&c[i]);
num[i]=1;
}
cnt=1;
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
start[u]=addedge(u,v);
pre[v]=u;
}
printf("%d\n",slove());
}
return 0;
}
hdu1055的更多相关文章
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu-1055(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1055 题意:给一棵树涂色,这棵树的每个节点都有自己的价值Ci,涂色的原则是只由这个节点的父节点涂色之后 ...
随机推荐
- WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...
- ajax技术返回json如何处理
json只是一种文本字符串. Smarty是一个使用PHP写出来的模板引擎. ajax如何处理json数据格式 ①json的格式如下: "{属性名:属性值,属性名:属性值}". 因 ...
- Progressive Web App是一个利用现代浏览器的能力来达到类似APP的用户体验的技术——不就是chrome OS吗?
什么是Progressive Web App? Progressive Web App是一个利用现代浏览器的能力来达到类似APP的用户体验的技术,由Google实现,让浏览器打开的网址像APP一样运行 ...
- WampServer 2.5设置外网访问/局域网手机访问(403 Forbidden错误解决方法)
安装好wamp后,想用手机通过局域访问电脑上wamp下的网页,结果出现如下提示. (403 Forbidden)错误 1.打开http.conf文件 2.找到下图中红色方框中的onlineofflin ...
- HDU3579Hello Kiki(中国剩余定理)(不互质的情况)
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...
- redisCheckMem脚本
最近维护的redis cluster需要扫描每个实例的内存使用率,首先我们需要获取实例已经使用的内存,获取实例的最大内存配额,两个值相比就能获取到内存使用比例. 实例的最大内存获取方法: $REDIS ...
- Swift错误处理
相对于可选中运用值的存在与缺失来表达函数的成功与失败,错误处理可以推断失败的原因,并传播至程序的其他部分. throws关键词 一个函数可以通过在声明中添加throws关键词来抛出错误消息. func ...
- 找工作--volatile
在Java编写的程序中,有时为了提高程序的运行效率,编译器会自动对其进行优化,把经常访问的变量缓存起来,程序在读取这个变量时有可能会直接从缓存(例如寄存器)中来读取这个值,而不会从内存中读取.这样做的 ...
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- LVS实战1
(一).NAT模式:NAT模型:地址转换类型,主要是做地址转换,类似于iptables的DNAT类型,它通过多目标地址转换,来实现负载均衡:特点和要求: 1.LVS(Director)上面需要双网卡: ...