长野原龙势流星群 题解
E_firework · · 题解
简单题。
考虑点权最大的一个节点,假设其为
code:
#include <bits/stdc++.h>
#define LL long long
#define Maxn 200005
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
int fa[Maxn], f[Maxn], c[Maxn];
LL s[Maxn];
double ans[Maxn];
int find(int i){
return i == f[i] ? i : f[i] = find(f[i]);
}
__int128 t1 = 1;
struct node{
LL a;
int b, id;
bool operator<(const node i)const{
return t1 * a * i.b < t1 * i.a * b;
}
};
priority_queue<node> q;
int main(){
int n = read();
for(int i = 2; i <= n; i++) fa[i] = read();
for(int i = 1; i <= n; i++) s[i] = read();
for(int i = 1; i <= n; i++){
f[i] = i, c[i] = 1;
q.push({s[i], 1, i});
}
int x, y;
while(q.size()){
x = q.top().id;
q.pop();
if(f[x] != x) continue;
ans[x] = 1.0 * s[x] / c[x];
y = find(fa[x]);
f[x] = y;
if(y){
s[y] += s[x];
c[y] += c[x];
q.push({s[y], c[y], y});
}
}
for(int i = 1; i <= n; i++) printf("%.12lf\n", ans[i]);
return 0;
}