2021.08.12【NOIP提高B组】模拟 树上摩托

34 篇文章 0 订阅
18 篇文章 0 订阅

在这里插入图片描述

思路:

首先得出结论,只有枚举出的大小能整除树的大小才可以分,然后直接筛出约束做,注意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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值