#include <bits/stdc++.h>
#define task ""
#define fi first
#define se second
#define pii pair<int, int>
using namespace std;
using ll = long long;
const ll mod = 998244353;
const int inf = 1e9 + 1;
const int arr = 300001;
int n, a, b, dp[arr], child[arr];
vector<int> adj[arr];
vector<int> trace;
void findpath(int u, int 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(int a, int b){
// return a > b;
// }
void dfs(int u, int p){
vector<int> 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<int>());
for(int i = 0; i < time.size(); i++){
dp[u] = max(dp[u], time[i] + i + 1);
}
}
pii calc(){
//memset(dp, 0, sizeof dp);
dfs(a, 0);
//memset(dp, 0, sizeof dp);
dfs(b, 0);
int ra = dp[a];
int rb = dp[b];
return {ra, rb};
}
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(int i = 1; i <= n; i++){
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
findpath(a, 0);
int l = 1, r = trace.size() - 1, res = 1e9;
while(l <= r){
int mid = (l + r) / 2;
child[trace[mid]] = trace[mid - 1];
child[trace[mid - 1]] = trace[mid];
pii tmp = calc();
child[trace[mid]] = child[trace[mid - 1]] = 0;
res = min(res, max(tmp.fi, tmp.se));
if(tmp.fi > tmp.se)r = mid - 1;
else l = mid + 1;
}
cout << res << '\n';
}
Compilation message
torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < time.size(); i++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
21588 KB |
Output is correct |
2 |
Correct |
276 ms |
22928 KB |
Output is correct |
3 |
Correct |
283 ms |
24444 KB |
Output is correct |
4 |
Correct |
317 ms |
23908 KB |
Output is correct |
5 |
Correct |
242 ms |
21048 KB |
Output is correct |
6 |
Correct |
245 ms |
21688 KB |
Output is correct |
7 |
Correct |
245 ms |
24820 KB |
Output is correct |