Submission #137040

# Submission time Handle Problem Language Result Execution time Memory
137040 2019-07-27T02:04:15 Z thebes Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1678 ms 185976 KB
#include <bits/stdc++.h>
using namespace std;

const int MN = 1e6+6;
int N, T, S, p[MN], dis[MN], dp[MN], u[MN], d[MN], m[MN], i, x, y;
vector<int> adj[MN], w[MN], path;

void dfs(int n,int pp,int dd){
    p[n]=pp; d[n]=dd;
    for(auto v : adj[n]){
        if(v==pp) continue;
        dfs(v, n, dd+1);
    }
}

void dfs2(int n,int pp){
    if(n != T){
        for(auto v : adj[n])
            if(!u[v]&&v!=pp) dis[n]++;
    }
    for(auto v : adj[n]){
        if(v != pp){
            dis[v] = dis[n];
            dfs2(v, n);
        }
    }
}

void dfs3(int n,int pp){
    int a=0, b=0;
    for(auto v : adj[n]){
        if(v==pp) continue;
        dfs3(v, n);
        if(dp[v]>a) b=a, a=dp[v];
        else if(dp[v]>b) b=dp[v];
    }
    dp[n]=max(dis[n],b);
}

int main(){
    for(scanf("%d%d%d",&N,&T,&S),i=1;i<N;i++){
        scanf("%d%d",&x,&y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dfs(S, 0, 1);
    for(i=T;i;i=p[i]){
        path.push_back(i);
        u[i] = 1;
    }
    dfs2(T, 0);
    for(auto v : path){
        m[d[v]] = dis[v];
        if(v == T) continue;
        for(auto a : adj[v]){
            if(u[a]) continue;
            dis[a] = 0;
            dfs2(a, v);
            dfs3(a, v);
            w[d[v]].push_back(dp[a]);
        }
    }
    for(i=1;i<=N;i++){
        sort(w[i].begin(),w[i].end(),[](int i,int j){return i>j;});
    }
    int lo=dis[S], hi=N;
    while(lo<hi){
        int mm=(lo+hi)/2;
        int ac=0, f=0, j, b=mm;
        for(i=1;i<=N;i++){
            for(j=-1;j+1<w[i].size();j++){
                if(w[i][j+1]+m[i]<=b) break;
            }
            ac-=j; b-=j+1;
            if(ac<0||b<0){
                f = 1; break;
            }
        }
        if(f) lo=mm+1;
        else hi=mm;
    }
    printf("%d\n",lo);
    return 0;
}

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:71:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(j=-1;j+1<w[i].size();j++){
                      ~~~^~~~~~~~~~~~
mousetrap.cpp:41:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%d%d%d",&N,&T,&S),i=1;i<N;i++){
         ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
mousetrap.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB Output is correct
2 Correct 45 ms 47352 KB Output is correct
3 Correct 44 ms 47352 KB Output is correct
4 Correct 44 ms 47352 KB Output is correct
5 Correct 45 ms 47356 KB Output is correct
6 Correct 44 ms 47352 KB Output is correct
7 Correct 46 ms 47352 KB Output is correct
8 Correct 55 ms 47352 KB Output is correct
9 Correct 45 ms 47352 KB Output is correct
10 Correct 45 ms 47352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 593 ms 94464 KB Output is correct
2 Correct 496 ms 89592 KB Output is correct
3 Correct 1519 ms 95448 KB Output is correct
4 Correct 710 ms 71560 KB Output is correct
5 Correct 1480 ms 95464 KB Output is correct
6 Correct 1542 ms 95512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB Output is correct
2 Correct 45 ms 47352 KB Output is correct
3 Correct 44 ms 47352 KB Output is correct
4 Correct 44 ms 47352 KB Output is correct
5 Correct 45 ms 47356 KB Output is correct
6 Correct 44 ms 47352 KB Output is correct
7 Correct 46 ms 47352 KB Output is correct
8 Correct 55 ms 47352 KB Output is correct
9 Correct 45 ms 47352 KB Output is correct
10 Correct 45 ms 47352 KB Output is correct
11 Correct 44 ms 47352 KB Output is correct
12 Correct 44 ms 47352 KB Output is correct
13 Correct 45 ms 47352 KB Output is correct
14 Correct 46 ms 47608 KB Output is correct
15 Correct 45 ms 47568 KB Output is correct
16 Correct 45 ms 47352 KB Output is correct
17 Correct 45 ms 47352 KB Output is correct
18 Correct 45 ms 47392 KB Output is correct
19 Correct 44 ms 47352 KB Output is correct
20 Correct 49 ms 47324 KB Output is correct
21 Correct 45 ms 47416 KB Output is correct
22 Correct 45 ms 47356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB Output is correct
2 Correct 45 ms 47352 KB Output is correct
3 Correct 44 ms 47352 KB Output is correct
4 Correct 44 ms 47352 KB Output is correct
5 Correct 45 ms 47356 KB Output is correct
6 Correct 44 ms 47352 KB Output is correct
7 Correct 46 ms 47352 KB Output is correct
8 Correct 55 ms 47352 KB Output is correct
9 Correct 45 ms 47352 KB Output is correct
10 Correct 45 ms 47352 KB Output is correct
11 Correct 593 ms 94464 KB Output is correct
12 Correct 496 ms 89592 KB Output is correct
13 Correct 1519 ms 95448 KB Output is correct
14 Correct 710 ms 71560 KB Output is correct
15 Correct 1480 ms 95464 KB Output is correct
16 Correct 1542 ms 95512 KB Output is correct
17 Correct 44 ms 47352 KB Output is correct
18 Correct 44 ms 47352 KB Output is correct
19 Correct 45 ms 47352 KB Output is correct
20 Correct 46 ms 47608 KB Output is correct
21 Correct 45 ms 47568 KB Output is correct
22 Correct 45 ms 47352 KB Output is correct
23 Correct 45 ms 47352 KB Output is correct
24 Correct 45 ms 47392 KB Output is correct
25 Correct 44 ms 47352 KB Output is correct
26 Correct 49 ms 47324 KB Output is correct
27 Correct 45 ms 47416 KB Output is correct
28 Correct 45 ms 47356 KB Output is correct
29 Correct 45 ms 47332 KB Output is correct
30 Correct 569 ms 107532 KB Output is correct
31 Correct 589 ms 107768 KB Output is correct
32 Correct 597 ms 162640 KB Output is correct
33 Correct 687 ms 185976 KB Output is correct
34 Correct 1644 ms 108792 KB Output is correct
35 Correct 1634 ms 108720 KB Output is correct
36 Correct 1678 ms 117084 KB Output is correct
37 Correct 1383 ms 116856 KB Output is correct
38 Correct 1148 ms 118904 KB Output is correct
39 Correct 1131 ms 119028 KB Output is correct
40 Correct 1217 ms 118968 KB Output is correct