#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500000
int *ej[N], eo[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;
}
long long x_, y_;
int dd[N], kk[N];
void dfs(int p, int i, int r) {
int o;
dd[i] = 0, kk[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
int d, k;
long long x, y;
dfs(i, j, r + 1);
d = dd[j] + 1, k = kk[j];
if (dd[i] > 0) {
x = (long long) (dd[i] + d) * r, y = (long long) kk[i] * kk[j];
if (x_ < x)
x_ = x, y_ = 0;
if (x_ == x)
y_ += y;
}
if (dd[i] < d)
dd[i] = d, kk[i] = 0;
if (dd[i] == d)
kk[i] += k;
}
}
}
int main() {
static int qu[N], qu_[N];
int n, n_, cnt, cnt_, r, h, i, j, d, d_, k, k_, k1, o;
long long x, y;
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);
}
memcpy(eo_, eo, n * sizeof *eo);
cnt = 0;
for (i = 0; i < n; i++)
if (eo[i] == 1)
qu[cnt++] = i;
n_ = n, r = 0;
while (n_ >= 3) {
cnt_ = 0;
for (h = 0; h < cnt; h++) {
i = qu[h];
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (--eo_[j] == 1)
qu_[cnt_++] = j;
}
}
n_ -= cnt, r++;
memcpy(qu, qu_, (cnt = cnt_) * sizeof *qu_);
}
x_ = 0, y_ = 0;
if (n_ == 1) {
i = qu[0], cnt = 0;
for (o = eo[i]; o--; ) {
j = ej[i][o];
dfs(i, j, r + 1);
d = dd[j] + 1;
if (d == r)
cnt++;
}
if (cnt == 2) {
d_ = 0, k_ = 0, k1 = 0;
for (o = eo[i]; o--; ) {
j = ej[i][o], d = dd[j] + 1, k = kk[j];
if (d == r)
k1 += kk[j];
else {
if (d_ < d)
d_ = d, k_ = 0;
if (d_ == d)
k_ += k;
}
}
if (d_ > 0) {
x = (long long) (r + d_) * r, y = (long long) k1 * k_;
if (x_ < x)
x_ = x, y_ = 0;
if (x_ == x)
y_ += y;
}
} else {
k1 = 0, y = 0;
for (o = eo[i]; o--; ) {
j = ej[i][o], d = dd[j] + 1, k = kk[j];
if (d == r)
k1 += kk[j], y -= (long long) kk[j] * kk[j];
}
y += (long long) k1 * k1, y /= 2;
x = (long long) (r + r) * r;
if (x_ < x)
x_ = x, y_ = 0;
if (x_ == x)
y_ += y;
}
} else {
i = qu[0], j = qu[1];
dfs(i, j, r + 1), dfs(j, i, r + 1);
}
if (x_ == 0)
y_ = 1;
printf("%lld %lld\n", x_, y_);
return 0;
}
Compilation message
road.c: In function 'append':
road.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
road.c: In function 'main':
road.c:54:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
road.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
688 KB |
Output is correct |
27 |
Correct |
1 ms |
692 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
728 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
560 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
544 KB |
Output is correct |
46 |
Correct |
1 ms |
540 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
688 KB |
Output is correct |
27 |
Correct |
1 ms |
692 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
728 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
560 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
544 KB |
Output is correct |
46 |
Correct |
1 ms |
540 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
336 ms |
45996 KB |
Output is correct |
50 |
Correct |
323 ms |
46124 KB |
Output is correct |
51 |
Correct |
333 ms |
45988 KB |
Output is correct |
52 |
Correct |
313 ms |
45992 KB |
Output is correct |
53 |
Correct |
266 ms |
45484 KB |
Output is correct |
54 |
Correct |
277 ms |
45464 KB |
Output is correct |
55 |
Correct |
271 ms |
45552 KB |
Output is correct |
56 |
Correct |
261 ms |
45516 KB |
Output is correct |
57 |
Correct |
309 ms |
49356 KB |
Output is correct |
58 |
Correct |
335 ms |
49580 KB |
Output is correct |
59 |
Correct |
333 ms |
49584 KB |
Output is correct |
60 |
Correct |
317 ms |
49452 KB |
Output is correct |
61 |
Correct |
348 ms |
54348 KB |
Output is correct |
62 |
Correct |
372 ms |
54356 KB |
Output is correct |
63 |
Correct |
320 ms |
42572 KB |
Output is correct |
64 |
Correct |
312 ms |
38676 KB |
Output is correct |
65 |
Correct |
312 ms |
36640 KB |
Output is correct |
66 |
Correct |
295 ms |
35484 KB |
Output is correct |
67 |
Correct |
317 ms |
35116 KB |
Output is correct |
68 |
Correct |
299 ms |
34992 KB |
Output is correct |
69 |
Correct |
287 ms |
34836 KB |
Output is correct |
70 |
Correct |
275 ms |
34764 KB |
Output is correct |
71 |
Correct |
286 ms |
34628 KB |
Output is correct |
72 |
Correct |
271 ms |
34844 KB |
Output is correct |
73 |
Correct |
302 ms |
34916 KB |
Output is correct |
74 |
Correct |
271 ms |
35020 KB |
Output is correct |
75 |
Correct |
280 ms |
35404 KB |
Output is correct |
76 |
Correct |
258 ms |
35936 KB |
Output is correct |
77 |
Correct |
219 ms |
37580 KB |
Output is correct |
78 |
Correct |
182 ms |
38860 KB |
Output is correct |