답안 #468207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468207 2021-08-27T06:42:53 Z idk321 Mousetrap (CEOI17_mousetrap) C++17
25 / 100
876 ms 77316 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 1000005;
vector<int> adj[N];
int upCost[N];
int downCost[N];
int p[N];
vector<vector<int>> sideNodes;

int n, t, m;

bool comp(int a, int b) {
    return downCost[a] < downCost[b];
}

void dfs(int node, int par) {
    p[node] = par;
    for (int next : adj[node]) {
        if (next == par) continue;
        dfs(next, node);
    }
}

void calcDown(int node, int depth) {
    if (adj[node].size() == 1) {
        downCost[node] = depth;
        return;
    }
    if (adj[node].size() == 2) {
        downCost[node] = depth + 1;
        return;
    }

    array<int, 2> most{0, 0};
    for (int next : adj[node]) {
        if (next == p[node]) continue;
        calcDown(next, depth + 1);
        if (downCost[next] > most[1]) most[1] = downCost[next];
        if (most[0] < most[1]) swap(most[0], most[1]);
    }

    downCost[node] = adj[node].size() - 2 + most[1];
}

void goUp(int node, int prev = -1) {
    if (node == t) return;
    goUp(p[node], node);
    upCost[node] += upCost[p[node]];
    if (p[node] != t) {
        upCost[node] += adj[p[node]].size();
        upCost[node] -= 2;
    }
    vector<int> curSideNodes;
    for (int next : adj[node]) {
        if (next == prev || next == p[node]) continue;
        curSideNodes.push_back(next);
        upCost[next] = upCost[node];
        calcDown(next, 1);
    }
    sort(curSideNodes.rbegin(), curSideNodes.rend(), comp);
    sideNodes.push_back(curSideNodes);
}



bool poss(int most) {
    int already = 0;
    int canHave = 1;
    for (auto it = sideNodes.rbegin(); it != sideNodes.rend(); it++) {
        vector<int>& nodesVector = *it;
        int blocked = 0;
        for (int i : nodesVector) {
            if (already + downCost[i] + upCost[i] + nodesVector.size() - blocked - 1 > most) {
                if (already < canHave) {
                    already++;
                    blocked++;
                } else return false;
            }
        }
        canHave++;
    }

    return true;
}


int bs() {
    int a = 0;
    int b = 2 * n;
    int res = -1;
    while (a <= b) {

        int mid =(a +b ) / 2;
        //cout << a<< " " << b << " "<< mid << " " << poss(mid) << "\n";
        if (poss(mid)) {
            res = mid;
            b = mid - 1;
        } else a = mid + 1;
    }

    return res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> t >> m;

    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(t, -1);
    goUp(m);

    /*
    for (int i = 1; i <= n; i++ )cout << upCost[i] << " ";
    cout << "\n";
    for (int i = 1; i <= n; i++ )cout << downCost[i] << " ";
    cout << "\n"; */
    cout << bs() << "\n";
}

/*
10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
*/

Compilation message

mousetrap.cpp: In function 'bool poss(int)':
mousetrap.cpp:76:86: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   76 |             if (already + downCost[i] + upCost[i] + nodesVector.size() - blocked - 1 > most) {
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 360 ms 62904 KB Output is correct
2 Correct 338 ms 70976 KB Output is correct
3 Correct 876 ms 77316 KB Output is correct
4 Correct 389 ms 50304 KB Output is correct
5 Correct 829 ms 77232 KB Output is correct
6 Correct 854 ms 77212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 23756 KB Output isn't correct
2 Halted 0 ms 0 KB -