#include <stdio.h>
#include <stdlib.h>
#define N 200000
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int ta[N], tb[N], sz[N];
void dfs(int p, int i) {
static int time;
int o;
sz[i] = 1;
ta[i] = time++;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
sz[i] += sz[j];
}
}
tb[i] = time;
}
int main() {
int n, h, i, j, ans;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
dfs(-1, 0);
ans = n + 1;
for (i = 1; i < n; i++)
for (j = i + 1; j < n; j++) {
int a, b, c;
if (ta[i] <= ta[j] && ta[j] < tb[i])
a = sz[j], b = sz[i] - sz[j], c = n - sz[i];
else if (ta[j] <= ta[i] && ta[i] < tb[j])
a = sz[i], b = sz[j] - sz[i], c = n - sz[j];
else
a = sz[i], b = sz[j], c = n - sz[i] - sz[j];
ans = min(ans, max(max(a, b), c) - min(min(a, b), c));
}
printf("%d\n", ans);
return 0;
}
Compilation message
papricice.c: In function 'append':
papricice.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
papricice.c: In function 'main':
papricice.c:41:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
papricice.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
12 ms |
436 KB |
Output is correct |
7 |
Correct |
9 ms |
420 KB |
Output is correct |
8 |
Correct |
10 ms |
440 KB |
Output is correct |
9 |
Correct |
7 ms |
332 KB |
Output is correct |
10 |
Correct |
11 ms |
436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
12 ms |
436 KB |
Output is correct |
7 |
Correct |
9 ms |
420 KB |
Output is correct |
8 |
Correct |
10 ms |
440 KB |
Output is correct |
9 |
Correct |
7 ms |
332 KB |
Output is correct |
10 |
Correct |
11 ms |
436 KB |
Output is correct |
11 |
Execution timed out |
1084 ms |
12884 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |