显然确定一个点的权值后整棵树权值确定。只要算出根节点的权值就能知道两种改法是否等价。
乘的话显然会炸,取log即可。map似乎会出一些问题,sort即可。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } #define N 500010 #define double long double const double eps=1E-10; int n,a[N],p[N],degree[N],t=0; double f[N]; struct data{int to,nxt; }edge[N<<1]; void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} void dfs(int k,int from) { for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) degree[k]++; for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) { f[edge[i].to]=f[k]+log(degree[k]); dfs(edge[i].to,k); } } int main() { #ifndef ONLINE_JUDGE freopen("bzoj3573.in","r",stdin); freopen("bzoj3573.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif n=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<n;i++) { int x=read(),y=read(); addedge(x,y),addedge(y,x); } dfs(1,1); int ans=0; for (int i=1;i<=n;i++) f[i]+=log(a[i]); sort(f+1,f+n+1); for (int i=1;i<=n;i++) { int t=i; while (t<n&&fabs(f[t+1]-f[i])<eps) t++; ans=max(ans,t-i+1); i=t; } cout<<n-ans; return 0; }