1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include<cstdio> #include<vector> #include<map> #define LL long long #define max(x,y) ((x)>(y)?(x):(y)) #define W while #define I inline #define ig(c) ('0'<=(c)&&(c)<='9') #define gc getchar I int read(){int x=0,f=1;char c=gc();W(!ig(c)) f=c=='-'?-1:f,c=gc();W(ig(c)) x=(x<<3)+(x<<1)+(c&15),c=gc();return x*f;} I void write(int x){x<0&&(putchar('-'),x=-x,0),x<10?(putchar(x+'0'),0):(write(x/10),putchar(x%10+'0'),0);} const int N=500010,mod=19260817; int n,a[N],f[N],ans; std::vector<int> v[N]; std::map<int,int> mp; I void dfs(int x,int fa,int sum){ int s=0;for(auto i:v[x]) (i^fa)&&(s++,0); ++mp[(int)(1ll*sum*a[x]%mod)]; ans=max(ans,mp[(int)(1ll*sum*a[x]%mod)]); sum=1ll*sum*s%mod; for(auto i:v[x]) (i^fa)&&(dfs(i,x,sum),0); } int main(){ n=read();for(int i=1;i<=n;i++) a[i]=read(); for(int x,y,i=1;i<n;i++) x=read(),y=read(),v[x].push_back(y),v[y].push_back(x); mp.clear();dfs(1,0,1ll); return write(n-ans),0; }
|