思路:
首先得出结论,只有枚举出的大小能整除树的大小才可以分,然后直接筛出约束做,注意dfs爆栈
c o d e code code
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n, ans, tot, head[1010110], v[1010101];
int siz[1010100], a[1010101], ru[1010101];
struct node
{
int to, next;
}b[10101011];
inline void add(register int x, register int y)
{
b[++tot]=(node){y, head[x]};
head[x]=tot;
}
inline void bfs()
{
queue<int> q;
q.push(1);
v[1]=1;
while(!q.empty())
{
register int xx=q.front();
q.pop();
for(register int i=head[xx]; i; i=b[i].next)
{
register int y=b[i].to;
if(v[y]==0)
{
q.push(y);
ru[xx]++;
v[y]=1;
}
}
}
}
inline void bfs1()
{
queue<int> q;
for(register int i=1; i<=n; i++)
if(!ru[i])
q.push(i), siz[i]=1;
while(!q.empty())
{
register int x=q.front();
q.pop();
for(register int i=head[x]; i; i=b[i].next)
{
register int y=b[i].to;
if(ru[y]==0)
continue;
ru[y]--;
siz[y]+=siz[x];
if(ru[y]==0)
siz[y]++, q.push(y);
}
}
}
inline int read()
{
register int x=0, f=1;
register char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int main()
{
n=read();
for(register int i=1; i<n; i++)
{
register int x, y;
x=read(), y=read();
add(y, x), add(x, y);
}
bfs();
bfs1();
tot=0;
for(register int i=1; i<=n; i++)
if(n%i==0)
a[++tot]=i;
for(register int i=1; i<=tot; i++)
{
register int sum=0;
for(register int j=1; j<=n; j++)
if(siz[j]%a[i]==0)
sum++;
if(sum==n/a[i])
ans++;
}
printf("%d", ans);
return 0;
}