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 <iostream>
#include <map>
#include <vector>
#include <array>
#include <queue>
#include <set>
const int MAX_N = 400 + 2;
const int MAX_H = 20 + 1;
std::array<std::vector<int>, MAX_N> graph;
std::array<int, MAX_N> parent, color, height;
std::array<std::pair<int, int>, MAX_N> interval;
std::map<int, int> toLeafIndex;
int leafCount;
std::array<int, (1 << MAX_H)> dp;
std::map<int, std::vector<int> > sameHeight;
void bfs(int k) {
std::queue<int> q;
height[0] = 0;
sameHeight[0].push_back(0);
q.push(0);
color[0] = true;
int index = 0;
while (!q.empty()) {
int n = q.front();
q.pop();
color[n] = true;
for (int c : graph[n]) {
if (!color[c]) {
parent[c] = n;
height[c] = height[n] + 1;
sameHeight[height[c]].push_back(c);
if (height[c] == k) {
toLeafIndex[c] = index;
index++;
continue;
}
q.push(c);
}
}
}
leafCount = index;
}
bool solve(int n, int k) {
bfs(k);
bool flag = false;
if (sameHeight[k].empty())
return true;
for (int i = 0; i < n; ++i) {
interval[i] = {1e9, -1};
}
for (int i : sameHeight[k]) {
int p = i;
do {
interval[p].first = std::min(interval[p].first, toLeafIndex[i]);
interval[p].second = std::max(interval[p].second, toLeafIndex[i]);
p = parent[p];
} while (p != 0);
}
dp[0] = -1;
for (int s = 1; s < (1 << k); ++s) {
for (int i = 0; i < k; ++i) {
if (s & (1 << i)) {
int l = dp[s ^ (1 << i)];
int nl = l;
for (int sh : sameHeight[i + 1]) {
if (interval[sh].first <= l + 1 && interval[sh].second >= l + 1) {
nl = std::max(nl, interval[sh].second);
}
}
dp[s] = std::max(dp[s], nl);
}
}
}
return dp[(1 << k) - 1] + 1 == leafCount;
}
int main() {
int n, k; std::cin >> n >> k;
for (int i = 0; i < n - 1; ++i) {
int a, b; std::cin >> a >> b; a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
if (k * k >= n) {
std::cout << "DA" << std::endl;
} else if (solve(n, k)) {
std::cout << "DA" << std::endl;
} else {
std::cout << "NE" << std::endl;
}
}
Compilation message (stderr)
burza.cpp: In function 'bool solve(int, int)':
burza.cpp:49:8: warning: unused variable 'flag' [-Wunused-variable]
49 | bool flag = false;
| ^~~~
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |