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"
#define SZ(x) (int)(x.size())
using namespace std;
const int mxN = 400 + 2;
int n, k, h[mxN], mark[mxN];
int l[mxN], r[mxN], cnt;
int link[mxN][20], dp[1 << 18][mxN];
vector<vector<int>> adj(mxN);
int dfs(int u, int par){
if(h[u] > k) mark[u] = 1;
int maxH = h[u];
for(int v : adj[u]){
if(v == par) continue ;
h[v] = h[u] + 1;
maxH = max(maxH, dfs(v, u));
}
if(maxH < k) mark[u] = 1;
return maxH;
}
void dfs2(int u, int par){
for(int v : adj[u]){
if(v == par) continue ;
h[v] = h[u] + 1;
dfs2(v, u);
if(l[u] == 0) l[u] = l[v];
r[u] = r[v];
}
if(h[u] == k){
cnt ++ ;
l[u] = cnt;
r[u] = cnt;
}
int depth = k - h[u];
for(int i = l[u]; i <= r[u]; i ++)
link[i][depth] = r[u];
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
vector<pair<int, int>> edges;
for(int i = 1; i <= n - 1; i ++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
edges.push_back({u, v});
}
dfs(1, 0);
vector<int> a;
for(int i = 1; i <= n; i ++){
if(mark[i] == 0) a.push_back(i);
h[i] = 0;
adj[i].clear();
}
for(auto [u, v] : edges){
if(mark[u] == 0 && mark[v] == 0){
int x = upper_bound(a.begin(), a.end(), u) - a.begin();
int y = upper_bound(a.begin(), a.end(), v) - a.begin();
adj[x].push_back(y);
adj[y].push_back(x);
}
}
n = a.size();
if(k * k >= n){
cout << "DA\n";
exit(0);
}
dfs2(1, 0);
dp[0][1] = 1;
for(int i = 1; i <= cnt; i ++){
for(int mask = 0; mask < (1 << k); mask ++){
if(dp[mask][i] == 0) continue ;
for(int x = ((1 << k) - 1) ^ mask; x > 0; x ^= x & (- x)){
int j = __builtin_ctz(x & (- x));
dp[mask | (1 << j)][link[i][j] + 1] = 1;
}
}
}
if(dp[(1 << k) - 1][cnt + 1] == 1) cout << "DA\n"; else cout << "NE\n";
return 0;
}
Compilation message (stderr)
burza.cpp: In function 'int main()':
burza.cpp:77:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | for(auto [u, v] : edges){
| ^
# | 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... |