Submission #932564

# Submission time Handle Problem Language Result Execution time Memory
932564 2024-02-23T15:52:23 Z LOLOLO Cat in a tree (BOI17_catinatree) C++17
0 / 100
73 ms 139776 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 2e5 + 10;
deque <int> dq[N];
int d;
vector <int> ed[N];
 
void merge(deque <int> &a, deque <int> &b) {
    for (int i = 0; i < sz(a); i++) {
        if (d - i < sz(b))
            b[i] = max({b[i], a[i] + b[max(d - i, i)], a[i]});         
    }

    for (int i = 0; i < sz(a); i++)
        b[i] = max(b[i], a[i]);
 
    for (int i = min(sz(b) - 2, sz(a) - 1); i >= 0; i--)
        b[i] = max(b[i], b[i + 1]);
}
 
void dfs(int u, int v) {
    dq[u].push_back(1);
    for (auto x : ed[u]) {
        if (x == v)
            continue;

        dfs(x, u);
        dq[x].push_front(dq[x][0]);
        if (sz(dq[u]) < sz(dq[x])) {
            swap(dq[u], dq[x]);
        }
 
        merge(dq[x], dq[u]);
    }
}
 
ll solve() {
    int n;
    cin >> n >> d;
    for (int i = 1; i < n; i++) {
        int x;
        cin >> x;
        ed[x].pb(i);
        ed[i].pb(x);
    }
 
    dfs(0, 0);
 
    return dq[0][0];
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int t = 1;
    //cin >> t;
 
    while (t--) {
        //solve();
        cout << solve() << '\n';
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 73 ms 139544 KB Output is correct
2 Incorrect 66 ms 139776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 73 ms 139544 KB Output is correct
2 Incorrect 66 ms 139776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 73 ms 139544 KB Output is correct
2 Incorrect 66 ms 139776 KB Output isn't correct
3 Halted 0 ms 0 KB -