玄关求调
查看原帖
玄关求调
773288
JackyNo1楼主2025/1/25 10:18
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5;
vector<int>g[N];
int dis[N];
int n, k, t, tot;
void dfs(int f, int x)
{
	dis[x] = 1;
	for (auto u : g[x])
	{
		if (u == f)continue;
		dfs(x, u);
		dis[x] += dis[u];
	}
	if (dis[x] == k)dis[x] = 0, tot++;
}
signed main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--)
	{
		memset(dis, 0, sizeof(dis));
		cin >> n >> k;
		for (int i = 1; i < n; i++)g[i].clear();
		for (int i = 1; i < n; i++)
		{
			int x, y;
			cin >> x >> y;
			g[x].push_back(y);
			g[y].push_back(x);
		}
		if (n % k != 0)
		{
			cout << "NO" << endl;
			continue;
		}
		dfs(1, 0);
		if (n / k != tot)cout << "NO" << endl;
		else cout << "YES" << endl;
	}
	return 0;
}
2025/1/25 10:18
加载中...