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>
#define N 70000
unsigned int Z = 12345;
int rand_() {
return (Z *= 3) >> 1;
}
int *ej[N], *ew[N], eo[N], n, d_;
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
void detach(int i, int j) {
int o;
for (o = eo[i]; o--; )
if (ej[i][o] == j) {
eo[i]--, ej[i][o] = ej[i][eo[i]], ew[i][o] = ew[i][eo[i]];
return;
}
}
int sz[N], n_, c;
void dfs1(int p, int i) {
int o, centroid;
sz[i] = 1, centroid = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j);
sz[i] += sz[j];
if (sz[j] * 2 > n_)
centroid = 0;
}
}
if ((n_ - sz[i]) * 2 > n_)
centroid = 0;
if (centroid)
c = i;
}
int ii[N], qu[N], cnt;
int pp[N], rr[N]; long long dd[N]; int kk[N];
void dfs2(int p, int i, int r, long long d) {
int o, h, lower, upper;
qu[cnt++] = i;
ii[n_++] = i;
pp[i] = p, rr[i] = r, dd[i] = d, sz[i] = 1, kk[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
if (j != p) {
dfs2(i, j, p == -1 ? j : r, d + w);
sz[i] += sz[j];
}
}
lower = -1, upper = cnt - 1;
while (upper - lower > 1) {
h = (lower + upper) / 2;
if (dd[i] - dd[qu[h]] > d_)
lower = h;
else
upper = h;
}
if (lower != -1)
kk[qu[upper]] += kk[i];
cnt--;
}
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = dd[ii[j]] != dd[i_] ? (dd[ii[j]] < dd[i_] ? -1 : 1) : sz[i_] - sz[ii[j]];
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
long long ans[N]; int kk_[N], ss[N];
void dfs3(int p, int i) {
int o, h, lower, upper;
qu[cnt++] = i;
lower = -1, upper = cnt - 1;
while (upper - lower > 1) {
h = (lower + upper) / 2;
if (dd[i] - dd[qu[h]] > d_)
lower = h;
else
upper = h;
}
kk_[i] += ss[qu[upper]];
ss[i] = (p == -1 ? 0 : ss[p]) + kk_[i];
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
kk_[j] -= ss[qu[upper]], dfs3(i, j);
}
if (p != -1)
ans[p] += (long long) kk_[i] * sz[i];
cnt--;
}
void cd(int n, int i) {
static int kk1[N];
int r, h, h1, h2, j, o, k;
n_ = n, dfs1(-1, i), r = c;
n_ = 0, dfs2(-1, r, r, 0);
for (h = 1; h < n; h++) {
i = ii[h];
ans[i] += (long long) (kk[i] - 1) * (n - sz[rr[i]]);
}
sort(ii, 0, n);
kk1[r] = 0;
for (o = eo[r]; o--; ) {
i = ej[r][o];
kk1[i] = 0;
}
for (h1 = 0, h2 = n - 1, k = 0; h1 < n; h1++) {
i = ii[h1];
while (h2 >= 0 && dd[i] + dd[j = ii[h2]] > d_) {
if (dd[j] <= d_)
k += kk[j], kk1[rr[j]] += kk[j];
h2--;
}
kk_[i] = k - kk1[rr[i]];
}
for (h = n - 1; h > 0; h--) {
i = ii[h];
kk_[i] -= kk_[pp[i]];
}
dfs3(-1, r);
for (o = eo[r]; o--; ) {
i = ej[r][o];
detach(i, r), cd(sz[i], i);
}
}
int main() {
int h, i, j, w;
scanf("%d%d", &n, &d_);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
for (h = 0; h < n - 1; h++) {
scanf("%d%d%d", &i, &j, &w);
append(i, j, w), append(j, i, w);
}
cd(n, 0);
for (i = 0; i < n; i++)
printf("%lld\n", ans[i]);
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Main.c: In function 'main':
Main.c:174:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | scanf("%d%d", &n, &d_);
| ^~~~~~~~~~~~~~~~~~~~~~
Main.c:180:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
180 | scanf("%d%d%d", &i, &j, &w);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |