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 n, a, b, isp[300010], cnt;
vector<int> e[300010], pth, v[300010];
int dfs(int x, int p){
isp[x] = 1;
if(x == b){
pth.push_back(x);
return 1;
}
for(auto &i : e[x]){
if(i == p) continue;
if(dfs(i, x)){
pth.push_back(x);
return 1;
}
}
isp[x] = 0;
return 0;
}
int f(int x, int p){
vector<int> v;
for(auto &i : e[x]){
if(i == p) continue;
v.push_back(f(i, x));
}
sort(v.begin(), v.end(), greater<int>());
int ret = 0, c = 0;
for(auto &i : v){
ret = max(ret, i + ++c);
}
return ret;
}
int pos(int k, int s, int d){
int t = 0;
for(; s >= 1 && s <= cnt; s += d){
int nt = t + 1;
for(auto &i : v[s]){
int lt = k - i - ++t;
if(lt < 0) return s;
if(lt == 0) nt = t + 1;
}
t = nt;
}
return s;
}
bool can(int k){
return pos(k, 1, 1) > pos(k, cnt, -1);
}
int main(){
scanf("%d%d%d", &n, &a, &b);
for(int i = 0; i < n - 1; i++){
int x, y; scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(a, 0);
for(auto &i : pth){
cnt++;
for(auto &j : e[i]){
if(isp[j]) continue;
v[cnt].push_back(f(j, i));
}
sort(v[cnt].begin(), v[cnt].end(), greater<int>());
}
int s = 0, e = n;
while(s <= e){
int m = (s + e) / 2;
if(can(m)) e = m - 1;
else s = m + 1;
}
printf("%d\n", s);
}
Compilation message (stderr)
torrent.cpp: In function 'int main()':
torrent.cpp:57:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &a, &b);
^
torrent.cpp:59:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d%d", &x, &y);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |