Submission #527878

# Submission time Handle Problem Language Result Execution time Memory
527878 2022-02-18T15:22:02 Z kaxzert Burza (COCI16_burza) C++17
160 / 160
62 ms 51516 KB
/**
      ⚡⚡  ⚡⚡      ⚡⚡⚡     ⚡⚡  ⚡⚡  ⚡⚡⚡⚡⚡⚡   ⚡⚡⚡⚡⚡  ⚡⚡⚡⚡⚡⚡  ⚡⚡⚡⚡⚡⚡
      ⚡⚡ ⚡⚡      ⚡⚡⚡⚡     ⚡⚡⚡⚡      ⚡⚡      ⚡⚡       ⚡⚡    ⚡⚡  ⚡⚡⚡⚡⚡⚡
      ⚡⚡⚡⚡      ⚡⚡  ⚡⚡      ⚡⚡      ⚡⚡       ⚡⚡⚡⚡⚡   ⚡⚡⚡⚡⚡⚡     ⚡⚡
      ⚡⚡ ⚡⚡    ⚡⚡⚡⚡⚡⚡     ⚡⚡     ⚡⚡         ⚡⚡       ⚡⚡  ⚡⚡       ⚡⚡
      ⚡⚡  ⚡⚡  ⚡⚡     ⚡⚡  ⚡⚡ ⚡⚡  ⚡⚡⚡⚡⚡⚡  ⚡⚡⚡⚡⚡    ⚡⚡   ⚡⚡     ⚡⚡
**/

#include<bits/stdc++.h>

using namespace std;

#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(i, a) for(auto &i : a)
#define sz(a) (int)a.size()
#define pi(a, b) pair<a, b>
#define fast ios::sync_with_stdio(false); cin.tie(0)

void setIO(string s) {
    if (sz(s) != 0) {
        freopen((s+".inp").c_str(),"r",stdin);
        freopen((s+".out").c_str(),"w",stdout);
    }
}

void setIOusaco(string s) {
    if (sz(s) != 0) {
        freopen((s+".in").c_str(),"r",stdin);
        freopen((s+".out").c_str(),"w",stdout);
    }
}

template<typename T, typename V>
bool ckmin(T &a, V b) {return (b < a)? a = b, true : false;}
template<typename T, typename V>
bool ckmax(T &a, V b) {return (b > a)? a = b, true : false;}

void print(int x) {cout << x;}
void print(long long x) {cout << x;}
void print(unsigned x) {cout << x;}
void print(unsigned long long x) {cout << x;}
void print(double x) {cout << fixed << x;}
void print(long double x) {cout << fixed << x;}
void print(char x) {cout << "'" << x << "'";}
void print(string x) {cout << '"' << x << '"';}
void print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void print(const pair<T, V> &x) {cout << '{'; print(x.ff); cout << ", "; print(x.ss); cout << '}';}
template<typename T>
void print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? ", " : ""), print(i); cout << "}";}
void _print() {cout << "]" << endl;}
template <typename T, typename... V>
void _print(T t, V... v) {print(t); if (sizeof...(v)) cout << ", "; _print(v...);}

#ifdef TAP
#define bug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define bug(x...) 
#endif

const int maxN = 408;
vt<vt<int> > f;
int par[maxN], n, k, maxr[maxN];
int jump[maxN][28];
vt<int> leaves;
vt<int> ke[maxN];

int dfs(int u, int p = 0) {
    int res = -1;
    par[u] = p;
    trav(v, ke[u]) {
        if (v == p) continue;
        ckmax(res, dfs(v, u));
    }
    return res+1;
}

void dfs1(int u, int p = 0, int depth = 0) {
    if (depth == k) {
        leaves.pb(u);
        return;
    }
    trav(v, ke[u]) {
        if (v == p) continue;
        dfs1(v, u, depth+1);
    }
}

void dfs2(int u, int p = 0) {
    trav(v, ke[u]) {
        if (v == p) continue;
        dfs2(v, u);
        ckmax(maxr[u], maxr[v]);
    }
}

int getbit(int k, int n) {
    return ((n >> k)&1);
}

int onbit(int k, int n) {
    return (n|(1<<k));
}

int dp(int u, int bitmask) {
    if (u == n+1) return 1;

    if (f[u][bitmask] != -1) return f[u][bitmask];

    fto(i, 0, k-1) {
        if (getbit(i, bitmask) == 0) {
            if (dp(jump[u][i]+1, onbit(i, bitmask))) return f[u][bitmask] = 1;
        }
    }
    return f[u][bitmask] = 0;
}

int main() {
    #ifndef TAP 
    setIO("");
    //setIOusaco("COCI16_burza");
    #endif

    fast;
    cin >> n >> k;
    if (k*k >= n) {
    	cout << "DA\n";
    	return 0;
    }

    fto(i, 1, n-1) {
    	int u, v;
    	cin >> u >> v;
    	ke[u].pb(v);
    	ke[v].pb(u);
    }

    if (k > dfs(1)) {
        cout << "DA\n";
        return 0;
    }

    dfs1(1);
    fto(i, 0, sz(leaves)-1) {
        maxr[leaves[i]] = i+1;
    }
    dfs2(1);

    fto(i, 0, sz(leaves)-1) {
        int li = leaves[i];
        fto(j, 0, k-1) {
            jump[i+1][j] = maxr[li];
            li = par[li];
        }
    }

    n = sz(leaves);
    f.assign(n+2, vt<int>((1<<k)+2, -1));

    cout << (dp(1, 0) ? "DA\n" : "NE\n");

    return 0;
}

Compilation message

burza.cpp: In function 'void setIO(std::string)':
burza.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         freopen((s+".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
burza.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen((s+".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
burza.cpp: In function 'void setIOusaco(std::string)':
burza.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen((s+".in").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
burza.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen((s+".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6604 KB Output is correct
2 Correct 45 ms 49956 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 43748 KB Output is correct
2 Correct 49 ms 42188 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 28 ms 42732 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 43212 KB Output is correct
2 Correct 46 ms 43212 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 17100 KB Output is correct
2 Correct 62 ms 51516 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 37068 KB Output is correct
2 Correct 48 ms 41164 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 45772 KB Output is correct
2 Correct 41 ms 40140 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 44180 KB Output is correct
2 Correct 48 ms 41676 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 37 ms 39116 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 12784 KB Output is correct
2 Correct 62 ms 47308 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 44 ms 47820 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 16972 KB Output is correct
2 Correct 54 ms 48332 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 12 ms 17992 KB Output is correct
6 Correct 1 ms 332 KB Output is correct