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 100000
#define Q 300000
int ds[N];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
}
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 dd[N], pp[N], pp_[N], qq[N], ta[N], tb[N];
int dfs1(int p, int i, int d) {
static int time;
int o, j_, k_, s;
ta[i] = time++;
dd[i] = d, pp[i] = pp_[i] = p;
s = 1, j_ = k_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
int k = dfs1(i, j, d + 1);
s += k;
if (k_ < k)
k_ = k, j_ = j;
}
}
qq[i] = j_;
tb[i] = time;
return s;
}
void dfs2(int p, int i, int q) {
int o, j_;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(i, j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && j != j_)
dfs2(i, j, j);
}
}
int lca(int i, int j) {
while (qq[i] != qq[j])
if (dd[qq[i]] > dd[qq[j]])
i = pp[qq[i]];
else
j = pp[qq[j]];
return dd[i] < dd[j] ? i : j;
}
char marked[N];
int find_(int i) {
return !marked[i] ? i : (pp_[i] = find_(pp_[i]));
}
int ft[N];
void update(int i, int n, int x) {
while (i < n) {
ft[i] += x;
i |= i + 1;
}
}
int query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int main() {
static int tt[Q], ii[Q], jj[Q], ans[Q];
int n, q, h, i, j, a;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
memset(ds, -1, n * sizeof *ds);
for (h = 0; h < q; h++) {
scanf("%d%d%d", &tt[h], &ii[h], &jj[h]), ii[h]--, jj[h]--;
ans[h] = find(ii[h]) == find(jj[h]) ? 0 : -1;
if (tt[h] == 1 && ans[h] == -1) {
join(ii[h], jj[h]);
append(ii[h], jj[h]), append(jj[h], ii[h]);
}
}
for (i = 0; i < n; i++)
if (ds[i] < 0)
dfs1(-1, i, 0), dfs2(-1, i, i);
for (h = 0; h < q; h++)
if (ans[h] != -1) {
i = ii[h], j = jj[h], a = lca(i, j);
if (tt[h] == 1) {
while (dd[i = find_(i)] > dd[a])
marked[i] = 1, update(ta[i], n, 1), update(tb[i], n, -1);
while (dd[j = find_(j)] > dd[a])
marked[j] = 1, update(ta[j], n, 1), update(tb[j], n, -1);
} else
ans[h] = (dd[i] + dd[j] - dd[a] * 2) - (query(ta[i]) + query(ta[j]) - query(ta[a]) * 2);
}
for (h = 0; h < q; h++)
if (tt[h] == 2)
printf("%d\n", ans[h]);
return 0;
}
Compilation message (stderr)
road_development.c: In function 'append':
road_development.c:33:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
33 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
road_development.c: In function 'main':
road_development.c:115:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
road_development.c:120:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | scanf("%d%d%d", &tt[h], &ii[h], &jj[h]), ii[h]--, jj[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |