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>
#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 (stderr)
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);
| ^~~~~
# | 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... |