Submission #1175900

#TimeUsernameProblemLanguageResultExecution timeMemory
1175900InvMODBurza (COCI16_burza)C++20
160 / 160
20 ms840 KiB
#include<bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()

void solve()
{
    int n,k; cin >> n >> k;

    vector<vector<int>> adj(n + 1, vector<int>());
    for(int i = 0; i < n - 1; i++){
        int u,v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    if(k * k >= n){
        // each h should has >= k element to counter Daniel
        cout << "DA\n";
        return;
    }

    int cntLose = 0;

    vector<int> tin(n + 1), tout(n + 1), h(n + 1);

    vector<vector<int>> cover(n + 1, vector<int>());
    function<void(int,int)> dfs = [&](int x, int p) -> void{
        if(h[x] >= k - 1){
            tin[x] = tout[x] = cntLose++;
            cover[tin[x]].push_back(x);
        }
        else{
            tin[x] = n + 1, tout[x] = -1;
            for(int v : adj[x])if(v != p){
                h[v] = h[x] + 1;
                dfs(v, x);

                tin[x] = min(tin[x], tin[v]);
                tout[x] = max(tout[x], tout[v]);
            }
            if(x != 1 && tin[x] <= n) cover[tin[x]].push_back(x);
        }
    };

    h[1] = -1; dfs(1, -1);

    /*
        + with each height we can only choose 1 element
        + choose an element x will cover form [tin[x] -> tout[x]]
    */

    vector<int> dp((1 << k), -1);

    dp[0] = 0;
    for(int i = 0; i < cntLose; i++){
        for(int mask = (1 << k) - 1; mask >= 0; mask--){
            if(dp[mask] >= i){
                for(int x : cover[i]){
                    if(mask >> h[x] & 1) continue;

                    dp[mask | (1 << h[x])] = max(dp[mask | (1 << h[x])], max(dp[mask], tout[x] + 1));
                }
            }
        }
    }

    cout << (*max_element(all(dp)) >= cntLose ? "DA" : "NE") << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    solve();
    return 0;
}

Compilation message (stderr)

burza.cpp: In function 'int32_t main()':
burza.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
burza.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...