#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 25;
vector <int> adj[MAXN];
int sze[MAXN], n, in[MAXN], out[MAXN], t;
typedef long long ll;
void dfs (int pos, int par) {
sze[pos] = 1;
in[pos] = ++t;
for (auto j : adj[pos]) {
if (j == par) continue;
dfs(j, pos);
sze[pos] += sze[j];
}
out[pos] = t;
}
bool check (int x, int y) {
return in[y] < in[x] && in[x] <= out[y];
}
int main () {
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
int mn = 1e9;
for (int i = 2; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (check(i, j)) {
int x = n - sze[j];
mn = min(mn, max({sze[i], sze[j] - sze[i], x}) - min({sze[i], sze[j] - sze[i], x}));
} else if (check(j, i)) {
int x = n - sze[i];
mn = min(mn, max({sze[i] - sze[j], sze[j], x}) - min({sze[i] - sze[j], sze[j], x}));
} else {
int x = n - sze[j] - sze[i];
mn = min(mn, max({sze[i], sze[j], x}) - min({sze[i], sze[j], x}));
}
}
}
cout << mn << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
9 ms |
7388 KB |
Output is correct |
7 |
Correct |
9 ms |
7256 KB |
Output is correct |
8 |
Correct |
10 ms |
7260 KB |
Output is correct |
9 |
Correct |
8 ms |
7260 KB |
Output is correct |
10 |
Correct |
10 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
9 ms |
7388 KB |
Output is correct |
7 |
Correct |
9 ms |
7256 KB |
Output is correct |
8 |
Correct |
10 ms |
7260 KB |
Output is correct |
9 |
Correct |
8 ms |
7260 KB |
Output is correct |
10 |
Correct |
10 ms |
7260 KB |
Output is correct |
11 |
Execution timed out |
1084 ms |
16152 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |