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>
using namespace std;
int main() {
//cin.tie(0)->sync_with_stdio(0);
int n,st,ed;
cin>>n>>st>>ed;
st--,ed--;
vector<set<int>> adjlist(n);
for(int i = 0; i < n - 1;i++)
{
int x,y;
cin>>x>>y;
x--,y--;
adjlist[x].insert(y),adjlist[y].insert(x);
}
vector<int> bet(n);
vector<int> path;
function<void(int,int)> fp = [&](int cur,int par) {
path.push_back(cur);
if(cur==ed){
return;
}
for(auto a:adjlist[cur])
if(a!=par) {
fp(a,cur);
if(path.back()==ed)
return;
}
path.pop_back();
};
fp(st,st);
function<int(int,int)> dfs = [&](int cur,int par) {
vector<int> vi;
for(auto a:adjlist[cur])
if(a!=par)
vi.push_back(dfs(a, cur));
sort(vi.begin(),vi.end(),greater<int>());
for(int i = 0; i < vi.size();i++)
vi[i] += i+1;
if(vi.empty())
return 0;
return *max_element(vi.begin(),vi.end());
};
int maxans = n;
int l =0 , r = path.size() - 2;
while(l<=r) {
int mid = (l+r)/2;
int x = path[mid], y = path[mid + 1];
adjlist[x].erase(y),adjlist[y].erase(x);
int a1 = dfs(st,st), a2 = dfs(ed,ed);
maxans = min(maxans, max(a1,a2));
if(a1>a2)
r = mid-1;
else if(a1 <= a2)
l= mid + 1;
adjlist[x].insert(y),adjlist[y].insert(x);
}
cout<<maxans<<"\n";
}
Compilation message (stderr)
torrent.cpp: In lambda function:
torrent.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i = 0; i < vi.size();i++)
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |