Submission #137343

# Submission time Handle Problem Language Result Execution time Memory
137343 2019-07-27T13:18:44 Z thebes Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1677 ms 186232 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 47356 KB Output is correct
3 Correct 45 ms 47300 KB Output is correct
4 Correct 45 ms 47352 KB Output is correct
5 Correct 46 ms 47452 KB Output is correct
6 Correct 46 ms 47352 KB Output is correct
7 Correct 45 ms 47352 KB Output is correct
8 Correct 45 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 563 ms 107652 KB Output is correct
2 Correct 516 ms 101880 KB Output is correct
3 Correct 1581 ms 108916 KB Output is correct
4 Correct 840 ms 77892 KB Output is correct
5 Correct 1473 ms 108628 KB Output is correct
6 Correct 1677 ms 108664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB Output is correct
2 Correct 45 ms 47356 KB Output is correct
3 Correct 45 ms 47300 KB Output is correct
4 Correct 45 ms 47352 KB Output is correct
5 Correct 46 ms 47452 KB Output is correct
6 Correct 46 ms 47352 KB Output is correct
7 Correct 45 ms 47352 KB Output is correct
8 Correct 45 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 45 ms 47412 KB Output is correct
12 Correct 46 ms 47392 KB Output is correct
13 Correct 45 ms 47352 KB Output is correct
14 Correct 55 ms 47480 KB Output is correct
15 Correct 45 ms 47420 KB Output is correct
16 Correct 45 ms 47352 KB Output is correct
17 Correct 46 ms 47352 KB Output is correct
18 Correct 45 ms 47480 KB Output is correct
19 Correct 46 ms 47452 KB Output is correct
20 Correct 46 ms 47356 KB Output is correct
21 Correct 46 ms 47352 KB Output is correct
22 Correct 45 ms 47352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB Output is correct
2 Correct 45 ms 47356 KB Output is correct
3 Correct 45 ms 47300 KB Output is correct
4 Correct 45 ms 47352 KB Output is correct
5 Correct 46 ms 47452 KB Output is correct
6 Correct 46 ms 47352 KB Output is correct
7 Correct 45 ms 47352 KB Output is correct
8 Correct 45 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 563 ms 107652 KB Output is correct
12 Correct 516 ms 101880 KB Output is correct
13 Correct 1581 ms 108916 KB Output is correct
14 Correct 840 ms 77892 KB Output is correct
15 Correct 1473 ms 108628 KB Output is correct
16 Correct 1677 ms 108664 KB Output is correct
17 Correct 45 ms 47412 KB Output is correct
18 Correct 46 ms 47392 KB Output is correct
19 Correct 45 ms 47352 KB Output is correct
20 Correct 55 ms 47480 KB Output is correct
21 Correct 45 ms 47420 KB Output is correct
22 Correct 45 ms 47352 KB Output is correct
23 Correct 46 ms 47352 KB Output is correct
24 Correct 45 ms 47480 KB Output is correct
25 Correct 46 ms 47452 KB Output is correct
26 Correct 46 ms 47356 KB Output is correct
27 Correct 46 ms 47352 KB Output is correct
28 Correct 45 ms 47352 KB Output is correct
29 Correct 45 ms 47352 KB Output is correct
30 Correct 555 ms 107756 KB Output is correct
31 Correct 564 ms 107640 KB Output is correct
32 Correct 642 ms 162784 KB Output is correct
33 Correct 663 ms 186232 KB Output is correct
34 Correct 1544 ms 108692 KB Output is correct
35 Correct 1517 ms 108728 KB Output is correct
36 Correct 1399 ms 117104 KB Output is correct
37 Correct 1398 ms 117076 KB Output is correct
38 Correct 1132 ms 119000 KB Output is correct
39 Correct 1178 ms 118896 KB Output is correct
40 Correct 1141 ms 118772 KB Output is correct