# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1026115 |
2024-07-17T15:25:39 Z |
Muhammet |
Torrent (COI16_torrent) |
C++17 |
|
448 ms |
27336 KB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define sz(x) (int)x.size()
#define ff first
#define ss second
const ll N = 300005;
const ll M = 1e9 + 7;
int T, n, a, b, c, d;
bool tr = 0, tr1 = 0;
vector <int> v[N], v1;
void dfs(int x, int y){
if(tr1 == 1) return;
for(auto i : v[x]){
if(i != y){
dfs(i,x);
}
}
if(x == b or tr == 1){
if(y == a) tr1 = 1;
tr = 1;
v1.push_back(y);
return;
}
}
int df(int x, int y){
vector <int> v2;
for(auto i : v[x]){
if((i != y) and (min(x,i) != min(c,d) or max(x,i) != max(c,d))){
v2.push_back(df(i,x));
}
}
sort(v2.rbegin(), v2.rend());
int mx = 0;
for(int i = 0; i < sz(v2); i++){
mx = max(mx, v2[i] + i + 1);
}
return mx;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> a >> b;
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
v1.push_back(b);
dfs(a,0);
v1.pop_back();
int l = 0, r = sz(v1)-2, k = 0;
while(l <= r){
int md = (l + r) / 2;
c = v1[md], d = v1[md+1];
if(df(a,0) < df(b,0)){
l = md+1;
k = md;
}
else {
r = md-1;
}
}
c = v1[k], d = v1[k+1];
int mn = max(df(a,0),df(b,0));
if(sz(v1) > 1){
c = v1[k+1], d = v1[k+2];
mn = min(mn,max(df(a,0),df(b,0)));
}
if(sz(v1) > 2){
c = v1[k+2], d = v1[k+3];
mn = min(mn,max(df(a,0),df(b,0)));
}
cout << mn;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
7516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
448 ms |
24776 KB |
Output is correct |
2 |
Correct |
378 ms |
27080 KB |
Output is correct |
3 |
Incorrect |
338 ms |
27336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |