/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = (int) 1e6;
int N, T, M;
vector <int> adj[N_MAX + 2];
int parent[N_MAX + 2];
int up[N_MAX + 2];
int down[N_MAX + 2];
void dfs (int u) {
int sons = (int) adj[u].size() - (u != T);
int max1 = 0, max2 = 0;
for (int v : adj[u]) {
if (v != parent[u]) {
parent[v] = u;
up[v] = up[u] + sons;
dfs(v);
if (down[v] >= max1) {
max2 = max1;
max1 = down[v];
} else if (down[v] > max2) {
max2 = down[v];
}
}
}
if (sons == 0) {
down[u] = up[u];
} else if (sons == 1) {
down[u] = up[u] + 1;
} else {
down[u] = max2;
}
}
int path[N_MAX + 2];
int len;
bool check (int answer) {
int block = 0;
for (int i = 1; i <= len; i++) {
int here = 0;
int u = path[i];
for (int v : adj[u]) {
if (v != parent[u] && v != path[i - 1]) {
if (block + down[v] > answer) {
here++;
}
}
}
block += here;
if (block > i) {
return false;
}
}
return true;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
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);
}
dfs(T);
int u = M;
while (u != T) {
path[++len] = u;
u = parent[u];
}
path[++len] = T;
for (int i = 1; i <= len; i++) {
int u = path[i];
for (int v : adj[u]) {
if (v != parent[u] && v != path[i - 1]) {
down[v] -= (len - i);
}
}
}
int l = 0, r = N;
while (l < r) {
int mid = (l + r) / 2;
if (check(mid) == true) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
283 ms |
80148 KB |
Output is correct |
2 |
Correct |
252 ms |
74428 KB |
Output is correct |
3 |
Correct |
683 ms |
81128 KB |
Output is correct |
4 |
Correct |
332 ms |
52312 KB |
Output is correct |
5 |
Correct |
696 ms |
81056 KB |
Output is correct |
6 |
Correct |
704 ms |
76760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |