/**
* 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 i, int j) {
return dfn[i] < dfn[j];
});
}
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] < at[k][mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
}
vector<vector<bool>> dp(sz + 1, vector<bool>(1 << (k + 1)));
vector<vector<int>> cnt(sz + 2, vector<int>(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 (cnt[i][s] > 0) {
dp[i][s] = true;
}
for (int idx : par[v]) {
if (idx == 0) {
continue;
}
if (s >> dep[idx] & 1) {
continue;
}
if (dp[i][s]) {
cnt[i][s | (1 << dep[idx])] += 1;
cnt[ptr[idx] + 2][s | (1 << dep[idx])] -= 1;
}
}
cnt[i + 1][s] += cnt[i][s];
}
}
bool ans = false;
for (int i = 0; i < (1 << (k + 1)); i++) {
ans = (ans | (cnt[sz][i] > 0));
}
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 |
Incorrect |
318 ms |
13652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1099 ms |
89476 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
88396 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
792 ms |
34388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1080 ms |
75724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1097 ms |
93768 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1093 ms |
90640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
613 ms |
25556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
168 ms |
10324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
833 ms |
34384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |