제출 #1344456

#제출 시각아이디문제언어결과실행 시간메모리
1344456OwstinBurza (COCI16_burza)C++20
0 / 160
58 ms864 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define pb push_back
#define all(x) begin(x), end(x)
#define umap unordered_map
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    int n, k; cin >> n >> k;
    vector<vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int x, y; cin >> x >> y; x--; y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    if (k * k >= n) {
        cout << "DE"; return;
    }
    vector<int> depth(n);
    vector<int> keep;
    vector<int> leaves;
    auto dfs = [&] (auto &&self, int node, int parent) -> bool {
        if (node) {
            depth[node] = depth[parent] + 1;
        }
        if (depth[node] == k) {
            adj[node].clear();
            adj[node].pb(parent);
            leaves.pb(node);
            return true;
        }
        bool res = false;
        for (auto x : adj[node]) {
            if (x == parent) {
                continue;
            }
            bool child = self(self, x, node);
            res = max(res, child);
            if (!node && child) {
                keep.pb(x);
            }
        }
        return res;
    };
    dfs(dfs, 0, -1);
    swap(keep, adj[0]);
    map<int, vector<int>> ancestors;
    auto dfs2 = [&] (auto &&self, int node, int parent, int start) -> bool {
        if (!node) {
            return true;
        }
        for (auto x : adj[node]) {
            if (x == parent) {
                continue;
            }
            if (self(self, x, node, start)) {
                ancestors[start].pb(node);
                return true;
            }
        }
        return false;
    };
    for (auto x : leaves) {
        dfs2(dfs2, x, -1, x);
    }
    vector<int> dp(1 << k, -1);
    for (int i = 1; i < 1 << k; i++) {
        for (int j = 0; j < k; j++) {
            if (i & (1 << j)) {
                int curr = dp[i ^ (1 << j)];
                if (curr == leaves.size() - 1) {
                    dp[i] = curr; break;
                }
                int need = ancestors[leaves[curr + 1]][j];
                int lo = curr + 1; int hi = leaves.size() - 1;
                while (lo < hi) {
                    int mid = (lo + hi + 1) / 2;
                    if (ancestors[leaves[mid]][j] == need) {
                        lo = mid;
                    }
                    else {
                        hi = mid - 1;
                    }
                }
                dp[i] = max(dp[i], lo);
            }
        }
    }
    cout << (dp.back() == leaves.size() - 1 ? "DA" : "NE");
}

int main() {
    FAST_IO;
    //freopen("guard.in", "r", stdin);
    //freopen("guard.out", "w", stdout);
    //TEST_CASES;
    solve(); cout << endl;
    /*int a; cin >> a;
    for (int i = 1; i <= a; i++){
        cout << "Case #" << i << ": ";
        solve();
        cout << endl;
    }*/
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...