Submission #289264

# Submission time Handle Problem Language Result Execution time Memory
289264 2020-09-02T13:49:42 Z evpipis Mousetrap (CEOI17_mousetrap) C++11
100 / 100
944 ms 170744 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef pair<int, int> ii;

const int len = 1e6+5, inf = 1e9;
vector<int> adj[len], cost[len], order;
int block[len], dp[2][len];

int fix_order(int u, int p, int en){
    if (u == en){
        block[u] = 1;
        return 1;
    }

    for (auto v: adj[u]){
        if (v == p) continue;

        if (fix_order(v, u, en)){
            order.pb(u);
            block[u] = 1;
            return 1;
        }
    }

    return 0;
}

int run_dfs(int u, int p){
    int mx1 = 0, mx2 = 0, kids = 0;
    for (auto v: adj[u]){
        if (v == p) continue;

        kids++;

        int cur = run_dfs(v, u);
        if (cur > mx1)
            mx2 = mx1, mx1 = cur;
        else if (cur > mx2)
            mx2 = cur;
    }

    return mx2+kids;
}

bool can_win(int x){
    int used = 0;
    for (int i = 0; i < (int)order.size(); i++){
        for (int j = 0; j < cost[i].size(); j++){
            if (cost[i][j]+used > x && used == i+1)
                return false;

            if (cost[i][j]+used > x)
                used++;
        }
    }

    if (used > x)
        return false;
    return true;
}

int bs(int l, int r){
    int ans;
    while (l <= r){
        int mid = (l+r)/2;
        if (can_win(mid))
            ans = mid, r = mid-1;
        else
            l = mid+1;
    }

    return ans;
}

int main(){
    //freopen("trap.01.05.in", "r", stdin);

    int n, en, st;
    scanf("%d %d %d", &n, &en, &st);
    for (int i = 0; i < n-1; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].pb(b);
        adj[b].pb(a);
    }


    fix_order(st, st, en);
    reverse(order.begin(), order.end());

    for (int i = (int)order.size()-1, cnt = 0; i >= 0; i--){
        int u = order[i];

        //printf("i = %d, u = %d\n", i, u);
        for (auto v: adj[u]){
            if (block[v]) continue;

            cost[i].pb(run_dfs(v, u)+1);

            //printf("%d -> %d, cost = %d\n", u, v, cost[i].back());
        }

        sort(cost[i].begin(), cost[i].end());
        reverse(cost[i].begin(), cost[i].end());

        for (int j = (int)cost[i].size()-1; j >= 0; j--)
            cost[i][j] += cnt++;

        /*printf("i = %d\n", i);
        for (int j = 0; j < cost[i].size(); j++)
            printf("%d ", cost[i][j]);
        printf("\n");*/
    }

    //printf("and answer is\n");
    printf("%d\n", bs(0, n));
    return 0;
}

Compilation message

mousetrap.cpp: In function 'bool can_win(int)':
mousetrap.cpp:53:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (int j = 0; j < cost[i].size(); j++){
      |                         ~~^~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |     scanf("%d %d %d", &n, &en, &st);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int bs(int, int)':
mousetrap.cpp:77:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   77 |     return ans;
      |            ^~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47224 KB Output is correct
2 Correct 30 ms 47232 KB Output is correct
3 Correct 31 ms 47232 KB Output is correct
4 Correct 30 ms 47232 KB Output is correct
5 Correct 32 ms 47232 KB Output is correct
6 Correct 31 ms 47232 KB Output is correct
7 Correct 31 ms 47232 KB Output is correct
8 Correct 31 ms 47232 KB Output is correct
9 Correct 32 ms 47224 KB Output is correct
10 Correct 30 ms 47232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 78584 KB Output is correct
2 Correct 374 ms 75512 KB Output is correct
3 Correct 875 ms 79864 KB Output is correct
4 Correct 428 ms 63736 KB Output is correct
5 Correct 897 ms 79736 KB Output is correct
6 Correct 876 ms 79748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47224 KB Output is correct
2 Correct 30 ms 47232 KB Output is correct
3 Correct 31 ms 47232 KB Output is correct
4 Correct 30 ms 47232 KB Output is correct
5 Correct 32 ms 47232 KB Output is correct
6 Correct 31 ms 47232 KB Output is correct
7 Correct 31 ms 47232 KB Output is correct
8 Correct 31 ms 47232 KB Output is correct
9 Correct 32 ms 47224 KB Output is correct
10 Correct 30 ms 47232 KB Output is correct
11 Correct 30 ms 47232 KB Output is correct
12 Correct 31 ms 47352 KB Output is correct
13 Correct 31 ms 47360 KB Output is correct
14 Correct 32 ms 47500 KB Output is correct
15 Correct 31 ms 47488 KB Output is correct
16 Correct 30 ms 47352 KB Output is correct
17 Correct 31 ms 47360 KB Output is correct
18 Correct 31 ms 47360 KB Output is correct
19 Correct 30 ms 47360 KB Output is correct
20 Correct 31 ms 47360 KB Output is correct
21 Correct 31 ms 47352 KB Output is correct
22 Correct 31 ms 47360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47224 KB Output is correct
2 Correct 30 ms 47232 KB Output is correct
3 Correct 31 ms 47232 KB Output is correct
4 Correct 30 ms 47232 KB Output is correct
5 Correct 32 ms 47232 KB Output is correct
6 Correct 31 ms 47232 KB Output is correct
7 Correct 31 ms 47232 KB Output is correct
8 Correct 31 ms 47232 KB Output is correct
9 Correct 32 ms 47224 KB Output is correct
10 Correct 30 ms 47232 KB Output is correct
11 Correct 467 ms 78584 KB Output is correct
12 Correct 374 ms 75512 KB Output is correct
13 Correct 875 ms 79864 KB Output is correct
14 Correct 428 ms 63736 KB Output is correct
15 Correct 897 ms 79736 KB Output is correct
16 Correct 876 ms 79748 KB Output is correct
17 Correct 30 ms 47232 KB Output is correct
18 Correct 31 ms 47352 KB Output is correct
19 Correct 31 ms 47360 KB Output is correct
20 Correct 32 ms 47500 KB Output is correct
21 Correct 31 ms 47488 KB Output is correct
22 Correct 30 ms 47352 KB Output is correct
23 Correct 31 ms 47360 KB Output is correct
24 Correct 31 ms 47360 KB Output is correct
25 Correct 30 ms 47360 KB Output is correct
26 Correct 31 ms 47360 KB Output is correct
27 Correct 31 ms 47352 KB Output is correct
28 Correct 31 ms 47360 KB Output is correct
29 Correct 29 ms 47232 KB Output is correct
30 Correct 416 ms 78724 KB Output is correct
31 Correct 423 ms 78712 KB Output is correct
32 Correct 508 ms 149224 KB Output is correct
33 Correct 479 ms 170744 KB Output is correct
34 Correct 896 ms 93048 KB Output is correct
35 Correct 897 ms 93176 KB Output is correct
36 Correct 916 ms 103024 KB Output is correct
37 Correct 944 ms 102976 KB Output is correct
38 Correct 757 ms 105072 KB Output is correct
39 Correct 779 ms 104688 KB Output is correct
40 Correct 746 ms 104692 KB Output is correct