#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
typedef vector<int> vi;
const int mx = 1e6;
vi adj[mx+1], road;
int n, t, m;
bool dfs (int u, int p) {
if (u==t) return 1;
for (int v : adj[u]) {
if (v==p) continue;
if (dfs (v, u)) {
road.pb(u);
return 1;
}
}
return 0;
}
int dfs2 (int u, int p) {
if (len(adj[u])<=2) return len(adj[u])-1;
vi nxt;
for (int v : adj[u]) {
if (v==p) continue;
nxt.pb(dfs2 (v, u));
}
sort(all(nxt),greater<int>());
return nxt[1]+len(adj[u])-1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> t >> m;
rep (i,1,n-1) {
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs (m,-1);
int l = len(road), s = 0, lo = 0, hi = mx;
vi dp[l];
reverse(all(road));
road.pb(t);
rrep (i,l-1,0) {
s += max(len(adj[road[i]])-3+(i==0),0);
for (int v : adj[road[i]]) {
if ((i>0 && v==road[i-1]) || v==road[i+1]) continue;
dp[i].pb(dfs2(v,road[i])+s+1);
}
sort(all(dp[i]), greater<int>());
s -= max(len(adj[road[i]])-3+(i==0),0);
s += max(len(adj[road[i]])-2+(i==0),0);
}
while (lo<hi) {
int mid = (lo+hi)/2, x = 0, suc = 1, cnt = 0;
rep (i,0,l-1) {
x++;
int nc = cnt;
for (int j : dp[i]) {
if (j+cnt>mid) {
x--;
nc++;
}
}
suc &= (x>=0);
cnt = nc;
}
if (suc && cnt<=mid) hi = mid;
else lo = mid+1;
}
cout << lo << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
15 ms |
23744 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23680 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
313 ms |
55352 KB |
Output is correct |
2 |
Correct |
265 ms |
51968 KB |
Output is correct |
3 |
Correct |
592 ms |
56320 KB |
Output is correct |
4 |
Correct |
262 ms |
39992 KB |
Output is correct |
5 |
Correct |
581 ms |
56108 KB |
Output is correct |
6 |
Correct |
582 ms |
56260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
15 ms |
23744 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23680 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23892 KB |
Output is correct |
15 |
Correct |
12 ms |
23812 KB |
Output is correct |
16 |
Correct |
13 ms |
23808 KB |
Output is correct |
17 |
Correct |
13 ms |
23808 KB |
Output is correct |
18 |
Correct |
12 ms |
23804 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
14 ms |
23732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
15 ms |
23744 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23680 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
313 ms |
55352 KB |
Output is correct |
12 |
Correct |
265 ms |
51968 KB |
Output is correct |
13 |
Correct |
592 ms |
56320 KB |
Output is correct |
14 |
Correct |
262 ms |
39992 KB |
Output is correct |
15 |
Correct |
581 ms |
56108 KB |
Output is correct |
16 |
Correct |
582 ms |
56260 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23820 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23892 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
13 ms |
23808 KB |
Output is correct |
23 |
Correct |
13 ms |
23808 KB |
Output is correct |
24 |
Correct |
12 ms |
23804 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
12 ms |
23812 KB |
Output is correct |
28 |
Correct |
14 ms |
23732 KB |
Output is correct |
29 |
Correct |
12 ms |
23764 KB |
Output is correct |
30 |
Correct |
305 ms |
68360 KB |
Output is correct |
31 |
Correct |
334 ms |
68452 KB |
Output is correct |
32 |
Correct |
344 ms |
119704 KB |
Output is correct |
33 |
Correct |
245 ms |
68560 KB |
Output is correct |
34 |
Correct |
608 ms |
69280 KB |
Output is correct |
35 |
Correct |
614 ms |
69468 KB |
Output is correct |
36 |
Correct |
645 ms |
77300 KB |
Output is correct |
37 |
Correct |
638 ms |
77316 KB |
Output is correct |
38 |
Correct |
505 ms |
79264 KB |
Output is correct |
39 |
Correct |
563 ms |
79220 KB |
Output is correct |
40 |
Correct |
496 ms |
79176 KB |
Output is correct |