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 st first
#define nd second
#define ll int64_t
using namespace std;
const int SZ=3e5+7;
vector <int> node[SZ];
int dp[SZ],GetA[SZ],GetB[SZ],a,b,go(0);
void dfs(int v,int p,int z) {
if(node[v].size()==1 && v!=a && v!=b) return ;
vector <int> went;
for(auto x:node[v]) {
if(x==p || x==z) continue;
dfs(x,v,z);
went.emplace_back(dp[x]);
}
int mx(0);
sort(went.begin(),went.end(),greater<int>());
for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
dp[v]=mx;
return ;
}
int main(void) {
cin.tie(0)->ios::sync_with_stdio(false);
int n,res((int)1e9);
cin >> n >> a >> b;
for(int i=1,x,y;i<n;i++) {
cin >> x >> y;
node[x].emplace_back(y);
node[y].emplace_back(x);
}
queue <int> hx;
hx.push(a);
GetA[a]=a;
while(!hx.empty()) {
auto o=hx.front();
hx.pop();
for(auto x:node[o]) {
if(!GetA[x]) {
GetA[x]=o;
hx.push(x);
}
}
}
int you=b;
while(you!=a) {
GetB[++go]=you;
you=GetA[you];
}
int l=1,h=go;
while(l<=h) {
int mi=(l+h)>>1;
dfs(a,-1,GetB[mi]);
dfs(b,-1,GetA[GetB[mi]]);
res=min(res,max(dp[a],dp[b]));
if(dp[a]>dp[b]) {
l=mi+1;
}else h=mi-1;
}
cout << res;
}
Compilation message (stderr)
torrent.cpp: In function 'void dfs(int, int, int)':
torrent.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |