Submission #1221691

#TimeUsernameProblemLanguageResultExecution timeMemory
1221691LucaIlieMousetrap (CEOI17_mousetrap)C++20
25 / 100
548 ms64312 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6;
const int MAX_N2 = 1e3;
const int INF = 1e9;
bool onPathTM[MAX_N + 1];
int parent[MAX_N + 1];
int dpArb[MAX_N + 1];
int f[MAX_N + 1];
vector<int> adj[MAX_N + 1];

void dfs(int u, int p) {
    parent[u] = p;
    for (int v: adj[u]) {
        if (v == p)
            continue;
        dfs(v, u);
    }
}

void calcDp(int u, int p) {
    int max1 = 0, max2 = 0;
    int sons = 0;
    for (int v: adj[u]) {
        if (v == p)
            continue;
        calcDp(v, u);

        if (!onPathTM[v]) {
            if (dpArb[v] > max1) {
                max2 = max1;
                max1 = dpArb[v];
            } else if (dpArb[v] > max2) 
                max2 = dpArb[v];
            sons++;
        }
    }

    dpArb[u] = sons + max2;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, t, m;

    cin >> n >> t >> m;
    for (int e = 0; e < n - 1; e++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(t, 0);

    int w = m;
    vector<int> path;
    while (w != t) {
        onPathTM[w] = true;
        path.push_back(w);
        w = parent[w];
    }
    onPathTM[w] = true;

    calcDp(t, 0);

    int totalSons = 0;
    vector<pair<int, int>> cand;
    int k = path.size();
    for (int i = m - 1; i >= 0; i--) {
        int u = path[i];
        for (int v: adj[u]) {
            if (onPathTM[v])
                continue;
            totalSons++;
            cand.push_back({dpArb[v], i});
        }
    }

    sort(cand.begin(), cand.end());
    reverse(cand.begin(), cand.end());

    for (auto p: cand) {
        int i = p.second;
        bool done = false;
        for (int j = i; j < k; j++) {
            f[j]++;
            done |= (f[j] > j + 1);
        }

        if (done) {
            cout << p.first + totalSons << "\n";
            return 0;
        }
    }

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