#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 1010
using namespace std;
struct node{
double avg;
int si,v,fa;
bool exist;
}nodes[N];
int find_fa(int x){
int fa = nodes[x].fa;
while(!nodes[fa].exist){
fa = nodes[fa].fa;
}
return fa;
}
int find_max(int r,int n){
int res;
double mx = -1;
for(int i=1;i<=n;i++){
if(i!=r&&nodes[i].exist&&nodes[i].avg>mx){
mx = nodes[i].avg;
res = i;
}
}
return res;
}
int main(){
struct node no;
int n,r;
scanf("%d %d",&n,&r);
while(n!=0&&r!=0){
for(int i=1;i<=n;i++){
scanf("%d",&nodes[i].v);
nodes[i].si = 1;
nodes[i].avg = nodes[i].v;
nodes[i].exist = true;
}
for(int i=1;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
nodes[b].fa = a;
}
for(int i=1;i<n;i++){
int indx = find_max(r,n);
int fa = find_fa(indx);
nodes[fa].v += (nodes[indx].avg*nodes[indx].si)*nodes[fa].si+
nodes[indx].v;
nodes[fa].avg = (nodes[fa].avg*nodes[fa].si+
nodes[indx].avg*nodes[indx].si)/(nodes[fa].si+nodes[indx].si);
nodes[fa].si += nodes[indx].si;
nodes[indx].exist = false;
}
printf("%d\n",nodes[r].v);
scanf("%d %d",&n,&r);
}
}
【树染色】 Color a tree
最新推荐文章于 2022-09-07 22:01:02 发布