First determine a root after Dfs again, count the number of points per subtree, and then enumerate the n factor x, count the number of sub-tree points number is a multiple of the current enumeration factor X, if there is a n/x subtree satisfies the condition, then answer +1
Code
1#include <cstdio>2#include <algorithm>3#include <Set>4 #defineFi first5 #defineSC Second6 #defineMP Make_pair7 #defineN 20001008 using namespacestd;9 intP[n],pre[n],tt[n],s[n],cnt[n];Ten intn,i,a,b,j,ans,dp,sum; One voidLinkintXinty) A { -dp++;p re[dp]=p[x];p [x]=dp;tt[dp]=y; - } the voidDfsintXintFA) - { - intI=P[x]; -s[x]=1; + while(i) - { + if(tt[i]!=FA) A { at DFS (tt[i],x); -s[x]+=S[tt[i]]; - } -I=Pre[i]; - } - } in intMain () - { toscanf"%d",&n); + for(i=1; i<n;i++) - { thescanf"%d%d",&a,&b); * Link (A, b); $ link (b,a);Panax Notoginseng } -Dfs1,0); the for(i=1; i<=n;i++) +cnt[s[i]]++; A for(i=1; i<=n;i++) the if(n%i==0) + { -sum=0; $ for(j=i;j<=n;j+=i) $sum+=Cnt[j]; - if(sum==n/i) ans++; - } theprintf"%d\n", ans); -}
Count of bzoj4401 blocks