답안 #250338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
250338 2020-07-17T13:17:48 Z Sorting Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1208 ms 160888 KB
#include <bits/stdc++.h>

using namespace std;

const int k_N = 1e6 + 3;

int n, trap, mouse;
int a[k_N], d[k_N], dp[k_N];
vector<int> adj[k_N];
vector<pair<int, int>> v;

void dfs(int u, int p){
    pair<int, int> ret;
    for(int to: adj[u]){
        if(to == p) continue;
        if(p != -1)
            dp[to] += dp[u] + adj[u].size() - 2;

        dfs(to, u);

        a[u] |= a[to];
        ret.second = max(dp[to], ret.second);
        if(ret.second > ret.first)
            swap(ret.first, ret.second);
    }

    if(adj[u].size() > 2)
        dp[u] = ret.second;
    dp[u] += adj[u].size() > 1;
}

void dfs2(int u, int p){
    if(u == trap)
        return;
    for(int to: adj[u]){
        if(to == p) continue;

        d[to] = d[u] + 1;
        dfs2(to, u);

        if(a[u] && !a[to])
            v.push_back({d[to], dp[to] + (u == mouse)});
    }
}

bool works(int x){
    int ret = 0;
    for(auto p: v){
        if(ret > p.first)
            return false;
        ret += (p.second + ret) > x;
    }
    return ret <= x;
}

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

    cin >> n >> trap >> mouse;

    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    a[mouse] = 1;
    d[mouse] = -1;

    dfs(trap, -1);
    dfs2(mouse, -1);

    sort(v.begin(), v.end());

    int l = 0, r = n;
    while(l != r){
        int mid = (l + r) >> 1;
        if(works(mid)) r = mid;
        else l = mid + 1;
    }

    cout << r << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
3 Correct 14 ms 23808 KB Output is correct
4 Correct 14 ms 23808 KB Output is correct
5 Correct 14 ms 23808 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 14 ms 23808 KB Output is correct
8 Correct 14 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 13 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 408 ms 65116 KB Output is correct
2 Correct 355 ms 60792 KB Output is correct
3 Correct 1043 ms 68088 KB Output is correct
4 Correct 451 ms 46080 KB Output is correct
5 Correct 1056 ms 67960 KB Output is correct
6 Correct 1208 ms 68180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
3 Correct 14 ms 23808 KB Output is correct
4 Correct 14 ms 23808 KB Output is correct
5 Correct 14 ms 23808 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 14 ms 23808 KB Output is correct
8 Correct 14 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 13 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 14 ms 23936 KB Output is correct
13 Correct 15 ms 23936 KB Output is correct
14 Correct 17 ms 23936 KB Output is correct
15 Correct 17 ms 23936 KB Output is correct
16 Correct 17 ms 23936 KB Output is correct
17 Correct 17 ms 23936 KB Output is correct
18 Correct 14 ms 23936 KB Output is correct
19 Correct 14 ms 23936 KB Output is correct
20 Correct 15 ms 23936 KB Output is correct
21 Correct 14 ms 23936 KB Output is correct
22 Correct 14 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
3 Correct 14 ms 23808 KB Output is correct
4 Correct 14 ms 23808 KB Output is correct
5 Correct 14 ms 23808 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 14 ms 23808 KB Output is correct
8 Correct 14 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 13 ms 23808 KB Output is correct
11 Correct 408 ms 65116 KB Output is correct
12 Correct 355 ms 60792 KB Output is correct
13 Correct 1043 ms 68088 KB Output is correct
14 Correct 451 ms 46080 KB Output is correct
15 Correct 1056 ms 67960 KB Output is correct
16 Correct 1208 ms 68180 KB Output is correct
17 Correct 14 ms 23808 KB Output is correct
18 Correct 14 ms 23936 KB Output is correct
19 Correct 15 ms 23936 KB Output is correct
20 Correct 17 ms 23936 KB Output is correct
21 Correct 17 ms 23936 KB Output is correct
22 Correct 17 ms 23936 KB Output is correct
23 Correct 17 ms 23936 KB Output is correct
24 Correct 14 ms 23936 KB Output is correct
25 Correct 14 ms 23936 KB Output is correct
26 Correct 15 ms 23936 KB Output is correct
27 Correct 14 ms 23936 KB Output is correct
28 Correct 14 ms 23808 KB Output is correct
29 Correct 14 ms 23808 KB Output is correct
30 Correct 390 ms 65016 KB Output is correct
31 Correct 400 ms 64888 KB Output is correct
32 Correct 473 ms 160888 KB Output is correct
33 Correct 501 ms 160820 KB Output is correct
34 Correct 1077 ms 68120 KB Output is correct
35 Correct 1054 ms 68056 KB Output is correct
36 Correct 987 ms 79524 KB Output is correct
37 Correct 1117 ms 79596 KB Output is correct
38 Correct 860 ms 78192 KB Output is correct
39 Correct 728 ms 78064 KB Output is correct
40 Correct 801 ms 78036 KB Output is correct