제출 #1120092

#제출 시각아이디문제언어결과실행 시간메모리
1120092tfgsTorrent (COI16_torrent)C++17
31 / 100
2015 ms31052 KiB
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int inf=1e18;

#ifdef LOCAL
#include "algo/debug.h"
#else
template <typename... Args>
void dummy(Args&&... args){}
#define ps dummy
#endif

#define f first
#define s second
template<class T> using V = vector<T>; 
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) 
#define len(x) (int)((x).size())
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ai2 array<int,2>
#define ai3 array<int,3>
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define pct __builtin_popcount
#define ctz __builtin_ctz
#define clz __builtin_clz
constexpr int p2(int x) { return (int)1 << x; }
constexpr int bits(int x) { return x == 0 ? 0 : 31-clz(x); } // floor(log2(x)) 
template<class T>void UNIQUE(V<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }
template<class T,class U>void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); }

int n,a,b;
vi del;
V<V<ai2>>g;
vi path_edge_inds;

bool find_path_to_b(int u,int p){
    if(u==b)return true;
    for(auto[v,ind]:g[u])if(v!=p&&!del[ind]){
        if(find_path_to_b(v,u)){
            path_edge_inds.pb(ind);
            return true;
        }
    }
    return false;
}

int calc_cost(int u,int p){
    vi subtree_costs;
    for(auto [v,ind]:g[u])if(v!=p&&!del[ind]){
        subtree_costs.pb(calc_cost(v,u));
    }
    sort(rall(subtree_costs));
    int cost=0;
    for(int i=0;i<len(subtree_costs);i++)
        ckmax(cost,i+1+subtree_costs[i]);
    return cost;
}

void solve() {
    cin>>n>>a>>b;a--;b--;
    g.rsz(n);
    del.rsz(n-1);
    for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;u--;v--;
        g[u].pb({v,i});
        g[v].pb({u,i});
    }
    if(a==b){
        int ans=calc_cost(a,-1);
        cout<<ans<<'\n';
        return;
    }

    int ans=inf;
    find_path_to_b(a,-1);
    for(int ind:path_edge_inds){
        del[ind]=true;
        ckmin(ans,max(calc_cost(a,-1),calc_cost(b,-1)));
        del[ind]=false;
    }

    cout<<ans<<'\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...