| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 846646 | vjudge1 | Torrent (COI16_torrent) | C++17 | 316 ms | 27376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define task "test1"
#define minpos(x, y) (a[x] <= a[y] ? (x) : (y))
#define pll pair<ll, ll>
#define pii pair<int, int>
using namespace std;
int const nmax = 3e5+3;
int const mod = 1e9+7;
int const LG = 20;
int const base=3;
void open_file()
{
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
}
void fast()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n,a,b,parent[nmax];
int use[nmax];
ll dp[nmax],ans=1e18;
vector<int> adj[nmax],v;
void dfs(int u,int par){
for(auto x : adj[u]){
if(x!=par){
parent[x]=u;
dfs(x,u);
}
}
}
void calc(int u,int par){
vector<ll> time;
dp[u]=0;
for(auto x : adj[u]){
if(x==use[u]) continue;
if(x!=par){
calc(x,u);
time.push_back(dp[x]);
}
}
sort(time.begin(),time.end(),greater<ll>());
for(int i=0;i<time.size();i++) dp[u]=max(dp[u],time[i]+i+1);
}
bool check(int mid){
use[v[mid]]=v[mid-1];
use[v[mid-1]]=v[mid];
calc(a,0);
calc(b,0);
use[v[mid]]=use[v[mid-1]]=0;
ans=min(ans,max(dp[a],dp[b]));
if(dp[a]<=dp[b]) return true;
return false;
}
void input()
{
cin >> n >> a >> b;
for(int i=1;i<n;i++){
int x,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
}
void NGU()
{
dfs(a,0);
int tmp=b;
while(tmp!=a){
v.push_back(tmp);
tmp=parent[tmp];
if(tmp==a){
v.push_back(a);
break;
}
}
reverse(v.begin(),v.end());
int l=1,r=v.size()-1;
while(l<=r){
int m=l+r>>1;
if(check(m)){
l=m+1;
}
else r=m-1;
}
cout << ans;
}
int main()
{
open_file();
fast();
input();
NGU();
}
/*
*/
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
