题面
洛谷
题解
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const double eps = 1e-8;
const int MAX_N = 5e5 + 5;
struct Graph { int to, next; } e[MAX_N << 1];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); }
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; }
int N, a[MAX_N], deg[MAX_N];
double f[MAX_N];
void dfs(int x, double s) {
f[x] = s + log(a[x]);
for (int i = fir[x]; ~i; i = e[i].next) dfs(e[i].to, s + log(deg[x]));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
clearGraph();
N = gi();
for (int i = 1; i <= N; i++) a[i] = gi();
for (int i = 1, u, v; i < N; i++) u = gi(), v = gi(), ++deg[u], Add_Edge(u, v);
dfs(1, 0);
sort(&f[1], &f[N + 1]);
int ans = 1;
for (int i = 2, cnt = 1; i <= N; i++) {
if (f[i] - f[i - 1] <= eps) ans = max(ans, ++cnt);
else cnt = 1;
}
printf("%d
", N - ans);
return 0;
}