本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
const int MAXN = 500011;
const int MAXM = 1000011;
const long double eps = 1e-8;//eps!!!
int n,a[MAXN],ecnt,first[MAXN],to[MAXM],next[MAXM],son[MAXN],ans;
long double p[MAXN];
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<‘0‘||c>‘9‘) && c!=‘-‘) c=getchar();
if(c==‘-‘) q=1,c=getchar(); while (c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w;
}
inline bool cmp(long double x,long double y){ if(fabs(x-y)<=eps) return true; return false; }
inline void dfs(int x,int fa){
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
dfs(v,x); son[x]++;
}
}
inline void dfs2(int x,int fa,long double lei){
lei+=log(son[x]);
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
p[v]=(long double)lei+log(a[v]);//不能用p[x]+!!!
dfs2(v,x,lei);
}
}
inline void work(){
n=getint(); for(int i=1;i<=n;i++) a[i]=getint(); int x,y;
for(int i=1;i<n;i++) {
x=getint(); y=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x;
}
dfs(1,0); dfs2(1,0,0); p[1]+=log(a[1]); sort(p+1,p+n+1); x=1;
for(int i=1;i<=n;i++) {
if(cmp(p[i],p[i-1])) x++;
else { ans=max(ans,x); x=1; }
}
printf("%d",n-ans);
}
int main()
{
work();
return 0;
}
原文:http://www.cnblogs.com/ljh2000-jump/p/6433406.html