Submission #950573

# Submission time Handle Problem Language Result Execution time Memory
950573 2024-03-20T12:43:01 Z GrindMachine Mousetrap (CEOI17_mousetrap) C++17
100 / 100
932 ms 201856 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
 
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
 
template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}
 
template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
 
/*
 
refs:
https://youtu.be/OH1U-ujZTuc?list=PLg_Krngrs0eC_1vn0dTjyPZMkwBOcVsgH
edi
 
*/
 
const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
 
vector<int> adj[N];
vector<int> curr_path, path;

void dfs1(int u, int p, int des){
    curr_path.pb(u);
    if(u == des){
        path = curr_path;
    }

    trav(v,adj[u]){
        if(v == p) conts;
        dfs1(v,u,des);
    }

    curr_path.pop_back();
}

vector<int> dp(N);
vector<bool> on_path(N);

void dfs2(int u, int p){
    vector<int> vals;

    trav(v,adj[u]){
        if(v == p or on_path[v]) conts;
        dfs2(v,u);
        vals.pb(dp[v]);
    }
    
    dp[u] = sz(vals);

    vals.pb(0), vals.pb(0); 
    sort(rall(vals));
    dp[u] += vals[1];
}
 
void solve(int test_case)
{
    int n,t,m; cin >> n >> t >> m;
    rep1(i,n-1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    dfs1(m,-1,t);

    trav(u,path) on_path[u] = 1;
    path.pop_back();

    vector<pii> branches;

    rep(i,sz(path)){
        int u = path[i];
        vector<int> vals;

        trav(v,adj[u]){
            if(on_path[v]) conts;
            dfs2(v,-1);
            vals.pb(dp[v]);
        }

        sort(rall(vals));
        trav(x,vals) branches.pb({i+1,x});
    }

    rep(i,sz(branches)){
        branches[i].ss += sz(branches)-i;
    }

    auto ok = [&](int mid){
        int b = 0;
        for(auto [d,val] : branches){
            if(b+val > mid){
                if(b >= d){
                    return false;
                }

                b++;
            }
        }

        return b <= mid;
    };

    int l = 0, r = 2*n;
    int ans = -1;

    while(l <= r){
        int mid = (l+r) >> 1;
        if(ok(mid)){
            ans = mid;
            r = mid-1;
        }
        else{
            l = mid+1;
        }
    }

    cout << ans << endl;
}
 
int main()
{
    fastio;
 
    int t = 1;
    // cin >> t;
 
    rep1(i, t) {
        solve(i);
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27740 KB Output is correct
2 Correct 7 ms 27964 KB Output is correct
3 Correct 7 ms 27736 KB Output is correct
4 Correct 7 ms 27740 KB Output is correct
5 Correct 7 ms 27740 KB Output is correct
6 Correct 7 ms 27740 KB Output is correct
7 Correct 7 ms 27740 KB Output is correct
8 Correct 8 ms 27740 KB Output is correct
9 Correct 7 ms 27992 KB Output is correct
10 Correct 8 ms 27740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 59220 KB Output is correct
2 Correct 255 ms 56144 KB Output is correct
3 Correct 855 ms 60244 KB Output is correct
4 Correct 369 ms 44128 KB Output is correct
5 Correct 867 ms 60240 KB Output is correct
6 Correct 932 ms 60712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27740 KB Output is correct
2 Correct 7 ms 27964 KB Output is correct
3 Correct 7 ms 27736 KB Output is correct
4 Correct 7 ms 27740 KB Output is correct
5 Correct 7 ms 27740 KB Output is correct
6 Correct 7 ms 27740 KB Output is correct
7 Correct 7 ms 27740 KB Output is correct
8 Correct 8 ms 27740 KB Output is correct
9 Correct 7 ms 27992 KB Output is correct
10 Correct 8 ms 27740 KB Output is correct
11 Correct 7 ms 27740 KB Output is correct
12 Correct 8 ms 27960 KB Output is correct
13 Correct 7 ms 27996 KB Output is correct
14 Correct 8 ms 27996 KB Output is correct
15 Correct 7 ms 27996 KB Output is correct
16 Correct 8 ms 27992 KB Output is correct
17 Correct 7 ms 27992 KB Output is correct
18 Correct 8 ms 27996 KB Output is correct
19 Correct 8 ms 27996 KB Output is correct
20 Correct 7 ms 27996 KB Output is correct
21 Correct 7 ms 28012 KB Output is correct
22 Correct 8 ms 27812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27740 KB Output is correct
2 Correct 7 ms 27964 KB Output is correct
3 Correct 7 ms 27736 KB Output is correct
4 Correct 7 ms 27740 KB Output is correct
5 Correct 7 ms 27740 KB Output is correct
6 Correct 7 ms 27740 KB Output is correct
7 Correct 7 ms 27740 KB Output is correct
8 Correct 8 ms 27740 KB Output is correct
9 Correct 7 ms 27992 KB Output is correct
10 Correct 8 ms 27740 KB Output is correct
11 Correct 309 ms 59220 KB Output is correct
12 Correct 255 ms 56144 KB Output is correct
13 Correct 855 ms 60244 KB Output is correct
14 Correct 369 ms 44128 KB Output is correct
15 Correct 867 ms 60240 KB Output is correct
16 Correct 932 ms 60712 KB Output is correct
17 Correct 7 ms 27740 KB Output is correct
18 Correct 8 ms 27960 KB Output is correct
19 Correct 7 ms 27996 KB Output is correct
20 Correct 8 ms 27996 KB Output is correct
21 Correct 7 ms 27996 KB Output is correct
22 Correct 8 ms 27992 KB Output is correct
23 Correct 7 ms 27992 KB Output is correct
24 Correct 8 ms 27996 KB Output is correct
25 Correct 8 ms 27996 KB Output is correct
26 Correct 7 ms 27996 KB Output is correct
27 Correct 7 ms 28012 KB Output is correct
28 Correct 8 ms 27812 KB Output is correct
29 Correct 7 ms 27996 KB Output is correct
30 Correct 302 ms 72536 KB Output is correct
31 Correct 280 ms 72528 KB Output is correct
32 Correct 310 ms 158940 KB Output is correct
33 Correct 408 ms 201856 KB Output is correct
34 Correct 849 ms 73624 KB Output is correct
35 Correct 823 ms 73500 KB Output is correct
36 Correct 845 ms 84112 KB Output is correct
37 Correct 817 ms 84196 KB Output is correct
38 Correct 667 ms 86212 KB Output is correct
39 Correct 692 ms 86140 KB Output is correct
40 Correct 686 ms 86120 KB Output is correct