Submission #726933

#TimeUsernameProblemLanguageResultExecution timeMemory
726933groguTorrent (COI16_torrent)C++14
0 / 100
640 ms20804 KiB
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
using namespace std;

#define maxn 300005
ll n,x,y;
vector<ll> g[maxn];
vector<ll> cur;
bool naso = 0;
void dfs1(ll u,ll p,ll en){
    cur.pb(u);
    if(u==en){naso = 1;return;}
    for(ll s : g[u]){
        if(s==p) continue;
        dfs1(s,u,en);
        if(naso) return;
    }
    cur.popb();
}
ll ans[maxn];
pll bad;
bool chk(ll x,ll y){
    if(x==bad.fi&&y==bad.sc) return 1;
    swap(x,y);
    if(x==bad.fi&&y==bad.sc) return 1;
    return 0;
}
ll dfs(ll u,ll p){
    vector<ll> v;
    for(ll s : g[u]){
        if(s==p) continue;
        if(chk(u,s)) continue;
        v.pb(dfs(s,u));
    }
    sort(all(v));
    reverse(all(v));
    ll ans = 0;
    for(ll i = 0;i<v.size();i++) ans = max(ans,v[i]+i+1);
    return ans;
}
ll f(ll i){
    if(ans[i]!=-1) return ans[i];
    bad = {cur[i],cur[i+1]};
    return max(dfs(x,x),dfs(y,y));
}
void tc(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> x >> y;
    for(ll i = 1;i<=n-1;i++){
        ll x,y; cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs1(x,x,y);
    ll l = 0,r = cur.size()-2,mid,rez = cur.size()-2;
    for(ll i = 0;i<=cur.size()-2;i++) ans[i] = -1;
    ans[cur.size()-1] = llinf;
    while(l<=r){
        ll mid = (l+r)/2;
        if(f(mid) - f(mid+1)<=0) rez = mid,r = mid-1;
        else l = mid+1;
    }
    cout<<f(rez)<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
	int t; t = 1;
	while(t--){
		tc();
	}
	return 0;
}
/**
6 2 1
1 2
2 3
2 4
1 5
5 6

10 1 2
1 2
2 5
1 3
1 4
4 6
6 7
3 8
3 9
3 10

17 1 2
1 3
1 4
4 6
6 7
3 8
3 9
3 10
1 13
13 5
13 11
13 12
13 14
14 15
15 16
15 17
14 2
**/

Compilation message (stderr)

torrent.cpp: In function 'long long int dfs(long long int, long long int)':
torrent.cpp:53:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for(ll i = 0;i<v.size();i++) ans = max(ans,v[i]+i+1);
      |                  ~^~~~~~~~~
torrent.cpp: In function 'void tc()':
torrent.cpp:71:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for(ll i = 0;i<=cur.size()-2;i++) ans[i] = -1;
      |                  ~^~~~~~~~~~~~~~
torrent.cpp:70:31: warning: unused variable 'mid' [-Wunused-variable]
   70 |     ll l = 0,r = cur.size()-2,mid,rez = cur.size()-2;
      |                               ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...