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 task ""
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<long long, long long>
using namespace std;
using ll = long long;
const ll mod = 998244353;
const int inf = 1e9 + 1;
const int arr = 1000001;
ll n, a, b, dp[arr], child[arr];
vector<ll> adj[arr];
vector<ll> trace;
void findpath(ll u, ll p) {
if(trace.empty() || trace.back() != b){
trace.push_back(u);
}
for(auto v : adj[u]) if(v != p)
findpath(v, u);
if(trace.back() != b)
trace.pop_back();
}
// bool cmp(ll a, ll b){
// return a > b;
// }
void dfs(ll u, ll p){
vector<ll> time;
dp[u] = 0;
for(auto v : adj[u]){
if(v != p && v != child[u]){
dfs(v, u);
time.push_back(dp[v]);
}
}
sort(time.begin(), time.end(), greater<ll>());
for(ll i = 0; i < time.size(); i++){
dp[u] = max(dp[u], time[i] + i + 1);
}
}
ll res = 1e18;
bool check(ll mid){
child[trace[mid]] = trace[mid - 1];
child[trace[mid - 1]] = trace[mid];
dfs(a, 0);
dfs(b, 0);
child[trace[mid]] = child[trace[mid - 1]] = 0;
res = min(res, max(dp[a], dp[b]));
return dp[a] < dp[b];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen(task".inp" , "r" , stdin);
// freopen(task".out" , "w" , stdout);
cin >> n >> a >> b;
for(ll i = 1; i <= n; i++){
ll x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
findpath(a, 0);
if(a == b) return cout << dp[a], 0;
ll l = 1, r = trace.size() - 1;
while(l <= r){
ll mid = (l + r) / 2;
if(check(mid))l = mid + 1;
else r = mid - 1;
}
cout << res << '\n';
}
Compilation message (stderr)
torrent.cpp: In function 'void dfs(ll, ll)':
torrent.cpp:40:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(ll i = 0; i < time.size(); i++){
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |