This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int root = 0; root < N; root++) {
vector<int> c(N);
auto color = [&](auto f, int x, int p, int dep) -> void {
if (dep % K == 0) {
c[x] = 1;
}
for (int y : adj[x]) {
if (y != p) {
f(f, y, x, dep + 1);
}
}
};
color(color, root, -1, 0);
auto dfs = [&](auto dfs, int x, int p, int cnt, int dep) -> bool {
if (dep == K - 1) {
return cnt == 1;
}
for (int y : adj[x]) {
if (y != p) {
if (!dfs(dfs, y, x, cnt + c[y], dep + 1)) {
return 0;
}
}
}
return 1;
};
bool ok = 1;
for (int i = 0; i < N; i++) {
if (!dfs(dfs, i, -1, c[i], 0)) {
ok = 0;
}
}
if (ok) {
cout << "YES\n1";
return 0;
}
}
cout << "NO\n0";
return 6/22;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |