Submission #1200086

#TimeUsernameProblemLanguageResultExecution timeMemory
1200086QuocSenseiBurza (COCI16_burza)C++20
160 / 160
41 ms992 KiB
#include <bits/stdc++.h>

#define ll long long
#define el cout << '\n'
#define BIT(n) (1ll << (n))
#define bit(mask, i) (((mask) >> (i)) & 1)
#define set_off(mask, i) ((mask) & ~BIT(i))

using namespace std;

const int maxn = 4e2;
const int maxk = 20;
const int INF = 1e9;

int n, k, l[maxn + 10], r[maxn + 10], dp[BIT(maxk)], dep[maxn + 10], cnt = 0, a[maxn + 10];
int nr[maxn + 10], nl[maxn + 10], ndep[maxn + 10];
vector<int> adj[maxn + 10];

void dfs(int top, int p = -1)
{
    if (dep[top] == k)
    {
        l[top] = r[top] = ++cnt;
        return ;
    }
    l[top] = INF;
    r[top] = 0;
    for (int next_top : adj[top])
    {
        if (next_top == p) continue;
        dep[next_top] = dep[top] + 1;
        dfs(next_top, top);
        l[top] = min(l[top], l[next_top]);
        r[top] = max(r[top], r[next_top]);
    }
}
bool cmp(int a, int b)
{
    if (l[a] == l[b]) return r[a] < r[b];
    return l[a] < l[b];
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen("BURZA.INP", "r"))
    {
        freopen("BURZA.INP", "r", stdin);
        freopen("BURZA.OUT", "w", stdout);
    }

    cin >> n >> k;
    if (k * k >= n) return cout << "DA", 0;
    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dfs(1);
    for (int i = 1; i <= n; i++)
        a[i] = i;
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        nl[i] = l[a[i]];
        nr[i] = r[a[i]];
        ndep[i] = dep[a[i]];
    }
    for (int mask = 0; mask < BIT(k); mask++)
        dp[mask] = -INF;
    dp[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        if (!ndep[i]) continue;
        for (int mask = BIT(k) - 1; mask >= 0; mask--)
            if (bit(mask, ndep[i] - 1) && dp[set_off(mask, ndep[i] - 1)] + 1 >= nl[i])
                dp[mask] = max({dp[mask], dp[set_off(mask, ndep[i] - 1)], nr[i]});
    }
    if (dp[BIT(k) - 1] == cnt) return cout << "DA", 0;
    cout << "NE";
}

Compilation message (stderr)

burza.cpp: In function 'int main()':
burza.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen("BURZA.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
burza.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen("BURZA.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...