/**
* author: wxhtzdy
* created: 11.08.2022 19:27:55
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
if (k >= 20) {
cout << "DA" << '\n';
return 0;
}
vector<int> dep(n);
vector<int> dfn(n);
vector<int> dfo(n);
int T = 0;
vector<vector<int>> par(n);
function<void(int, int)> Dfs = [&](int v, int pr) {
dfn[v] = ++T;
for (int p : par[pr]) {
par[v].push_back(p);
}
par[v].push_back(v);
for (int u : g[v]) {
if(u == pr) {
continue;
}
dep[u] = dep[v] + 1;
Dfs(u, v);
}
dfo[v] = T;
};
Dfs(0, 0);
vector<vector<int>> at(n);
for (int i = 0; i < n; i++) {
at[dep[i]].push_back(i);
}
for (int i = 0; i < n; i++) {
sort(at[i].begin(), at[i].end(), [&](int x, int y) {
return dfn[x] < dfn[y];
});
}
auto isPar = [&](int v, int u) {
return dfn[v] <= dfn[u] && dfo[u] <= dfo[v];
};
vector<int> ptr(n);
int sz = (int) at[k].size();
for (int i = 1; i < n; i++) {
if (dep[i] > k) {
continue;
}
int low = 0, high = sz - 1;
while (low <= high) {
int mid = low + high >> 1;
if (isPar(i, at[k][mid])) {
ptr[i] = mid;
low = mid + 1;
} else {
if (dfn[i] < dfn[at[k][mid]]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
}
vector<vector<bool>> dp(sz + 1, vector<bool>(1 << (k + 1)));
dp[0][0] = true;
for (int i = 0; i < sz; i++) {
int v = at[k][i];
for (int s = 0; s < (1 << (k + 1)); s++) {
if (!dp[i][s]) {
continue;
}
for (int idx : par[v]) {
if (idx == 0) {
continue;
}
if (s >> dep[idx] & 1) {
continue;
}
if (dp[i][s]) {
dp[ptr[idx] + 1][s | (1 << dep[idx])] = true;
}
}
}
}
bool ans = false;
for (int i = 0; i < (1 << (k + 1)); i++) {
ans = (ans | dp[sz][i]);
}
cout << (ans ? "DA" : "NE") << '\n';
return 0;
}
Compilation message
burza.cpp: In function 'int main()':
burza.cpp:67:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int mid = low + high >> 1;
| ~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
724 KB |
Output is correct |
2 |
Correct |
54 ms |
3412 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
3028 KB |
Output is correct |
2 |
Correct |
53 ms |
2900 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
55 ms |
2984 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
3028 KB |
Output is correct |
2 |
Correct |
61 ms |
3028 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1364 KB |
Output is correct |
2 |
Correct |
69 ms |
3540 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
2624 KB |
Output is correct |
2 |
Correct |
56 ms |
2900 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
3184 KB |
Output is correct |
2 |
Correct |
57 ms |
2820 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
3084 KB |
Output is correct |
2 |
Correct |
52 ms |
2900 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
58 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1108 KB |
Output is correct |
2 |
Correct |
58 ms |
3284 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
596 KB |
Output is correct |
2 |
Correct |
55 ms |
3284 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
1376 KB |
Output is correct |
2 |
Correct |
63 ms |
3284 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
27 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |