이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//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... |