上学期putsnan过了一次,这学期认真写了一遍……
#include<bits/stdc++.h> #define N 500010 using namespace std; struct Edge{int u,v,next;}G[N<<1]; int head[N],tot=0,n,k,size[N]; double dp[N]; inline void addedge(int u,int v){ G[++tot].u=u;G[tot].v=v;G[tot].next=head[u];head[u]=tot; } void dfs(int u){ size[u]++;bool flag=0; for(int i=head[u];i;i=G[i].next){ int v=G[i].v;dfs(v);size[u]+=size[v];flag=1; } if(!flag){dp[u]=1;return;} for(int i=head[u];i;i=G[i].next){ int v=G[i].v; dp[u]=max(dp[u],min(dp[v],(double)size[v]/(double)(size[u]-1))); } } inline int read(){ int f=1,x=0;char ch; do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9'); do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9'); return f*x; } int main(){ n=read();k=read(); for(int i=2;i<=n;i++){int u=read();addedge(u,i);} dfs(1);double ans=0; for(int i=1;i<=n;i++)if(size[i]>k&&ans<dp[i])ans=dp[i]; printf("%.7lf",ans); return 0; }