#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N) for(int i = 0; i < (N); ++i)
#define rrep(i, N) for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N) for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef DEBUG
#define debug(x...) { \
++dbg::depth; \
string dbg_vals = dbg::to_string(x); \
--dbg::depth; \
dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
}
#define light_debug(x) { \
dbg::light = true; \
dbg::dout << __func__ << ":" << __LINE__; \
dbg::dout << " " << #x << " = " << x << endl; \
dbg::light = false; \
}
#else
#define debug(x...) 42
#define light_debug(x) 42
#endif
using ll = long long;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
if(k * k >= n)
return cout << "DA\n", 0;
vector<vector<int>> adj(n);
rep(i, n - 1) {
int u, v; cin >> u >> v; --u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
vector<int> in(n), out(n), at_dist_k, p(n), ord;
y_combinator([&](auto dfs, int u, int d, int pr) -> void {
static int t{};
p[u] = pr;
in[u] = t++;
ord.push_back(u);
for(int v: adj[u]) if(v != pr)
dfs(v, d + 1, u);
out[u] = t;
if(d == k)
at_dist_k.push_back(u);
})(0, 0, -1);
vector<int> last(n);
int m{int(at_dist_k.size())};
{
int i = 0;
rep(j, n) {
while(i < m && in[at_dist_k[i]] < j) ++i;
last[ord[j]] = i;
}
}
debug(in, out, ord);
debug(p);
debug(at_dist_k);
debug(last);
vector<vector<bool>> dp(m + 1, vector<bool>(1 << k));
fill(all(dp[0]), true);
rep1(i, m) {
rep(levels, 1 << k) {
for(int u = at_dist_k[i - 1], x = 1 << (k - 1); x > 0; x >>= 1, u = p[u]) {
if(levels & x)
dp[i][levels] = dp[i][levels] || dp[last[u]][levels ^ x];
}
}
}
cout << (dp.back().back() ? "DA" : "NE") << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: "
<< clock() * 1000 / CLOCKS_PER_SEC
<< "ms" << endl;
#endif
return 0;
}
Compilation message
burza.cpp: In function 'int main()':
burza.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
burza.cpp:108:5: note: in expansion of macro 'debug'
108 | debug(in, out, ord);
| ^~~~~
burza.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
burza.cpp:109:5: note: in expansion of macro 'debug'
109 | debug(p);
| ^~~~~
burza.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
burza.cpp:110:5: note: in expansion of macro 'debug'
110 | debug(at_dist_k);
| ^~~~~
burza.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
burza.cpp:111:5: note: in expansion of macro 'debug'
111 | debug(last);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
524 KB |
Output is correct |
2 |
Correct |
768 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
691 ms |
1660 KB |
Output is correct |
2 |
Correct |
629 ms |
1616 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
660 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
684 ms |
1640 KB |
Output is correct |
2 |
Correct |
693 ms |
1732 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
820 KB |
Output is correct |
2 |
Correct |
805 ms |
1908 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
584 ms |
1456 KB |
Output is correct |
2 |
Correct |
641 ms |
1584 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
698 ms |
1720 KB |
Output is correct |
2 |
Correct |
607 ms |
1556 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
708 ms |
1680 KB |
Output is correct |
2 |
Correct |
623 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
593 ms |
1516 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
716 KB |
Output is correct |
2 |
Correct |
763 ms |
1780 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
472 KB |
Output is correct |
2 |
Correct |
758 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
836 KB |
Output is correct |
2 |
Correct |
753 ms |
1808 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
265 ms |
844 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |