BZOJ 4401: 块的计数

Description

小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树。听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效。当然,无聊的小Y对这种事情毫无兴趣,只是对把树分块这个操作感到十分好奇。他想,假如能把一棵树分成几块,使得每个块中的点数都相同该有多优美啊!小Y很想知道,能有几种分割方法使得一棵树变得优美。小Y每次会画出一棵树,但由于手速太快,有时候小Y画出来的树会异常地庞大,令小Y感到十分的苦恼。但是小Y实在是太想知道答案了,于是他找到了你,一个天才的程序员,来帮助他完成这件事。

Input

第一行一个正整数N,表示这棵树的结点总数,接下来N-1行,每行两个数字X,Y表示编号为X的结点与编号为Y的结点相连。结点编号的范围为1-N且编号两两不同。

Output

一行一个整数Ans,表示所求的方案数。

Sample Input

6

1 2

2 3

2 4

4 5

5 6

Sample Output

3

HINT

100%的数据满足N<=1000000。

分析

显然块的大小必然是n的因子。不难发现如果块的大小确定了,那么分块的方案最多只会有一种。
这里还有个小结论,就是说,一棵树能分成大小为b的若干块当且仅当有n/b棵子树的size是b的倍数。

代码

#include <bits/stdc++.h>

#define N 1000005

int max(int x,int y)
{
    return x > y ? x : y;
}

int min(int x,int y)
{
    return x < y ? x : y;
}

int read()
{
    int x = 0, f = 1;
    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;
}

struct Edge
{
    int to,next;
}e[N * 2];

int next[N];
int cnt;

void add(int x,int y)
{
    e[++cnt] = (Edge){y, next[x]}; next[x] = cnt;
    e[++cnt] = (Edge){x, next[y]}; next[y] = cnt;
}

int size[N];
int n;

void dfs(int x,int fa)
{
    size[x] = 1;
    for (int i = next[x]; i; i = e[i].next)
    {
        if (e[i].to == fa)
            continue;
        dfs(e[i].to, x);
        size[x] += size[e[i].to];
    }
}

int t[N];

bool check(int x)
{
    int tot = 0;
    for (int i = x; i <= n; i += x)
        tot += t[i];
    return tot == n / x;
}

int main()
{
    n = read();
    for (int i = 1; i < n; i++)
    {
        int x = read(), y = read();
        add(x, y);
    }
    dfs(1,0);
    for (int i = 1; i <= n; i++)
        t[size[i]]++;
    int ans = 0;
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if (check(i))
                ans++;
            if (i * i != n && check(n / i))
                ans++;
        }
    }
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值