Submission #1339545

#TimeUsernameProblemLanguageResultExecution timeMemory
1339545fatelessCat in a tree (BOI17_catinatree)C++20
0 / 100
0 ms344 KiB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
 
#define now chrono::steady_clock::now().time_since_epoch().count()
#define speedup cin.tie(0)->sync_with_stdio(0)
#define bitcount __builtin_popcount
#define all(x) begin(x), end(x)
#define pb push_back
#define vc vector

using ld = double;
using pii = array<int, 2>;

const int inf = 1e9;
inline bool chmin(int &a, int b) {if (a > b) {a = b; return 1;} return 0;}
inline bool chmax(int &a, int b) {if (a < b) {a = b; return 1;} return 0;}
inline void solve() {
    int n, D;
    cin >> n >> D;
    vc<vc<int>> adj(n);
    for (int b = 1; b < n; b++) {
        int a; cin >> a;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    auto good = [&](int mask) -> bool {
        int st = __lg(mask);

        // bitset<20> t(mask);
        // cout << t << '\n';

        vc<int> d (n, -1);
        queue<int> q;
        q.push(st);
        d[st] = 0;
        while (q.size()) {
            int a = q.front(); q.pop();
            for (int b : adj[a]) {
                if (d[b] != -1) continue;
                if (mask >> b & 1) {
                    if (d[a] + 1 < D) return 0;
                    d[b] = 0;
                } else d[b] = d[a] + 1;
                q.push(b);
            }
        }
        // for (int i : d) cout << i << ' ';
        // cout << "\n\n";
        return 1;
    };

    int ans = 0;
    for (int mask = 1; mask < (1 << n); mask++) 
        if (good(mask)) chmax(ans, bitcount(mask));
    
    cout << ans;
}

signed main() {
    speedup;
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...