Submission #870274

# Submission time Handle Problem Language Result Execution time Memory
870274 2023-11-07T11:52:34 Z sleepntsheep Cat in a tree (BOI17_catinatree) C++17
0 / 100
0 ms 348 KB
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using i64 = long long;
#define N 200005

int n, d, l[N], h[N], alloc = 1, dp[N][2], t[N<<1];
pair<int, int> g[N<<1];

void upd(int p, int k) {
    p += 3;for (; p ; p-=p&-p) t[p] += k;
}
int qry(int p) { p += 3; int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; }

void add_edge(int u, int v)
{
    int p = alloc++;
    g[p] = {v, h[u]};
    h[u] = p;
}

void dfs(int u, int p, int dep)
{
    l[u] = dep;
    int base = 0, lowest = 1e9;
    for (auto j = h[u]; j; j = g[j].second)
    {
        int v = g[j].first;
        if (v == p) continue;
        dfs(v, u, dep+1);
        base += dp[v][0] - 1;
        upd(dp[v][1], 1);
        lowest = min(lowest, dp[v][1]);
    }
    dp[u][0] = base, dp[u][1] = lowest;

    {
        int sel = base + 1 + qry(d + dep);
        if (dp[u][0] < sel) dp[u][0] = sel, dp[u][1] = dep;
        else if (dp[u][0] == sel && dep > dp[u][1]) dp[u][1] = dep;
    }

    for (auto j = h[u]; j; j = g[j].second)
    {
        int v = g[j].first;
        if (v == p) continue;
        int delta = dp[v][1] - dep;
        int safe = (d+1)/2;
        if (delta >= safe)
        {
            int sel = base + qry(dep + safe);
            if (dp[u][0] < sel)
                dp[u][0] = sel, dp[u][1] = dp[v][1];
            else if (dp[u][0] == sel && dp[v][1] > dp[u][1])
                dp[u][1] = dp[v][1];
        }
        else
        {
            int sel = base + 1 + qry(dep + max(safe, d- delta));
            if (dp[u][0] < sel)
                dp[u][0] = sel, dp[u][1] = dp[v][1];
            else if (dp[u][0] == sel && dp[v][1] > dp[u][1])
                dp[u][1] = dp[v][1];
        }
    }

    for (auto j = h[u]; j; j = g[j].second)
    {
        int v = g[j].first;
        if (v != p) upd(dp[v][1], -1);
    }
}

int main()
{
    ShinLena;
    cin >> n >> d;
    for (int i = 1, v; i < n; ++i) cin >> v, add_edge(i, v), add_edge(v, i);
    dfs(0, 0, 1);
    for (int i = 0; i < n; ++i)
    {
        //cerr << dp[i][0] << ' '<< dp[i][1] << ' ' <<l[i] <<endl;
    }
    cout << dp[0][0];

    return 0;
}


# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -