语文题。。。
原来除了hash还可以取对数啊orz
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> #include<cstdlib> #define maxv 500500 #define maxe 1000500 using namespace std; int n,val[maxv],son[maxv],x,y,g[maxv],nume=0,fath[maxv],ret,ans=0; double r[maxv],stack[maxv],mx; queue <int> q; struct edge { int v,nxt; }e[maxe]; void addedge(int u,int v) { e[++nume].v=v; e[nume].nxt=g[u]; g[u]=nume; } void bfs() { while (!q.empty()) q.pop(); q.push(1);r[1]=0; while (!q.empty()) { int head=q.front();q.pop();son[head]=0; for (int i=g[head];i;i=e[i].nxt) { int v=e[i].v; if (v!=fath[head]) { fath[v]=head; son[head]++; q.push(v); } } for (int i=g[head];i;i=e[i].nxt) { int v=e[i].v; if (v!=fath[head]) r[v]=r[head]+log(son[head]); } } } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&val[i]); for (int i=1;i<=n-1;i++) { scanf("%d%d",&x,&y); addedge(x,y); addedge(y,x); } bfs(); for (int i=1;i<=n;i++) stack[i]=r[i]+log(val[i]); sort(stack+1,stack+n+1); ret=1; for (int i=2;i<=n+1;i++) { if (fabs(stack[i-1]-stack[i])<1e-5) ret++; else {ans=max(ans,ret);ret=1;} } printf("%d\n",n-ans); return 0; }