#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
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 |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
651 ms |
49628 KB |
Output is correct |
2 |
Correct |
578 ms |
51244 KB |
Output is correct |
3 |
Correct |
602 ms |
52992 KB |
Output is correct |
4 |
Correct |
556 ms |
52544 KB |
Output is correct |
5 |
Correct |
657 ms |
49440 KB |
Output is correct |
6 |
Correct |
674 ms |
50156 KB |
Output is correct |
7 |
Correct |
539 ms |
53832 KB |
Output is correct |