#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e6 + 50;
ll n, t, m, val[N], dp[N], ps[N], k;
bool mark[N];
vector<int> adj[N];
vector<int> vp;
vector<pii> vc;
void dfs(int u, int p=0) {
cerr << "YES " << u << endl;
if (u == t) return;
int mx1 = 0, mx2 = 0;
for (int v:adj[u]) {
if (v == p) continue;
dfs(v, u);
if (mark[v]) {
mark[u] = 1;
val[u] = val[v] + (int)adj[u].size()-1-(p>0);
}
else {
if (dp[v] > mx1) {
mx2 = mx1;
mx1 = dp[v];
}
else if (dp[v] > mx2) {
mx2 = dp[v];
}
}
}
cerr << "OK " << endl;
if (!mark[u]) {
dp[u] = (int)adj[u].size()-(p>0) + mx2;
}
else {
vp.push_back(u);
for (int v:adj[u]) {
if (v == p) continue;
if (mark[v]) continue;
vc.push_back({u, dp[v] + val[u]});
}
}
cerr << "OK " << endl;
}
bool isval(ll x) {
int id = 0;
int tmp = 0;
for (pii p:vc) {
while (id < p.F) {
ps[id+1] = ps[id];
tmp = ps[id];
id++;
}
if (tmp+p.S > x) {
ps[id]++;
}
}
for (int i = 1; i <= k; i++) {
if (ps[i] > i || ps[i] > x) return 0;
}
return 1;
}
void solve() {
cin >> n >> t >> m;
for (int i = 1; i <= n-1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
mark[t] = 1;
dfs(m);
cerr << "OK " << endl;
reverse(vp.begin(), vp.end());
reverse(vc.begin(), vc.end());
for (pii &x : vc) {
if (x.F != vp[k]) k++;
x.F = k+1;
}
k = vp.size();
ll dw = -1, up = 1e9;
while (up-dw > 1) {
ll md = (up+dw)>>1;
if (isval(md)) up = md;
else dw = md;
}
cout << up << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23740 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23728 KB |
Output is correct |
7 |
Correct |
13 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23716 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5016 ms |
72044 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23740 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23728 KB |
Output is correct |
7 |
Correct |
13 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23716 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
20 ms |
23884 KB |
Output is correct |
13 |
Correct |
19 ms |
23880 KB |
Output is correct |
14 |
Correct |
21 ms |
23964 KB |
Output is correct |
15 |
Correct |
19 ms |
23968 KB |
Output is correct |
16 |
Correct |
20 ms |
23880 KB |
Output is correct |
17 |
Correct |
20 ms |
23892 KB |
Output is correct |
18 |
Correct |
23 ms |
23884 KB |
Output is correct |
19 |
Correct |
19 ms |
23892 KB |
Output is correct |
20 |
Correct |
19 ms |
23896 KB |
Output is correct |
21 |
Correct |
20 ms |
23892 KB |
Output is correct |
22 |
Correct |
17 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23740 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23728 KB |
Output is correct |
7 |
Correct |
13 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23716 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Execution timed out |
5016 ms |
72044 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |