This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |