【BZOJ4401】块的计数

【题目链接】

【思路要点】

  • 补档博客,无题解。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	1000005
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
vector <int> a[MAXN];
int n, size[MAXN], cnt[MAXN];
void work(int pos, int fa) {
	size[pos] = 1;
	for (unsigned i = 0; i < a[pos].size(); i++)
		if (a[pos][i] != fa) {
			work(a[pos][i], pos);
			size[pos] += size[a[pos][i]];
		}
	cnt[size[pos]]++;
}
bool check(int x, int goal) {
	int all = 0;
	for (int i = x; i <= n; i += x)
		all += cnt[i];
	return all == goal;
}
int main() {
	read(n);
	for (int i = 1; i < n; i++) {
		int x, y;
		read(x), read(y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	work(1, 0);
	int ans = 0;
	for (int i = 1; i * i <= n; i++) {
		if (n % i == 0) if (i * i == n) ans += check(i, i);
		else ans += check(i, n / i), ans += check(n / i, i);
	}
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/80523014