Submission #1210347

#TimeUsernameProblemLanguageResultExecution timeMemory
1210347thdh__Mousetrap (CEOI17_mousetrap)C++20
100 / 100
391 ms123068 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll

using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

/*
Competitive Programming notes that I need to study & fix my dumbass self:

1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops, I don't know
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you

2. Stress:
- Don't be cocky and think stressing with your dumbass brute-force solution will give you guaranteed AC. 
- Always try generating big testcases and try if they run

3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time

I hate offline because I am dumb
*/

typedef pair<int, int> ii;
const int N = 1e6+5;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

int n,t,m;
vector<int> adj[N];
int dp[N], par[N];

void dfs(int u, int p) 
{
    int mx = 0, sec = 0;
    for (auto v : adj[u]) 
    {
        if (v == p) continue;
        par[v] = u;
        dfs(v,u);
        if (dp[v] >= mx) sec = mx, mx = dp[v];
        else if (dp[v] > sec) sec = dp[v];
    }
    dp[u] = sec + adj[u].size() - 1;
}

int sum = 0;

bool check(int x) 
{
    int u = m, rem = 0, child = -1, cnt = 0, tot = 0;
    while (u != t) 
    {
        rem++;
        int add = 0;
        for (auto v : adj[u]) 
        {   
            if (v == child || v == par[u]) continue;
            if ((dp[v] + cnt) > (x + tot)) 
            {
                if (!rem) return 0;
                else rem--, add++;
            } 
        }   
        cnt += add;
        tot += adj[u].size() - (child != -1);
        if (par[u] != 0) tot--;
        child = u, u = par[u];
    }
    sum = tot;
    return 1;
}

void solve()
{
	cin>>n>>t>>m;
    for (int i = 1; i < n; i++) 
    {
        int u,v; cin>>u>>v;
        adj[u].pb(v), adj[v].pb(u);
    }
    dfs(t,0);
    int l = -1, r = n;
    while (l < r-1) 
    {
        int mid = l+r>>1;
        if (check(mid)) r = mid;
        else l = mid;
    }
    cout<<r + sum;
}

/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/

signed main()
{
	bruh
	//freopen("input.inp","r",stdin);
	//freopen("output.inp","w",stdout);
	solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...