This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bitmask DP Solution
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ms(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
//===========================================
const int MAX = 405;
vector<int> adj[MAX];
int dep[MAX], dp[1<<19], l[MAX], r[MAX], curr, n, k;
void dfs(int v, int p){
if (dep[v] == k){ l[v] = r[v] = ++curr; return; }
for (int u: adj[v]){
if (u == p) continue;
dep[u] = dep[v]+1;
dfs(u, v);
l[v] = min(l[v], l[u]);
r[v] = max(r[v], r[u]);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
ms(l, INF); ms(r, 0);
if (k >= 20){ cout << "NE\n"; return 0; }
for (int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1, -1);
for (int mask = 1; mask < 1<<k; mask++){
dp[mask] = -INF;
for (int i = 1; i <= n; i++){
if (!dep[i] || !(mask&(1<<(dep[i]-1)))) continue;
if (dp[mask^(1<<(dep[i]-1))]+1 >= l[i]) dp[mask] = max({dp[mask], r[i], dp[mask^(1<<(dep[i]-1))]});
}
}
cout << (dp[(1<<k)-1] == curr? "DA\n" : "NE\n");
}
# | 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... |