#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
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 |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
17228 KB |
Output is correct |
2 |
Correct |
159 ms |
16964 KB |
Output is correct |
3 |
Correct |
159 ms |
20636 KB |
Output is correct |
4 |
Correct |
143 ms |
26056 KB |
Output is correct |
5 |
Correct |
149 ms |
22212 KB |
Output is correct |
6 |
Correct |
146 ms |
20596 KB |
Output is correct |
7 |
Correct |
169 ms |
17072 KB |
Output is correct |
8 |
Correct |
153 ms |
23200 KB |
Output is correct |
9 |
Correct |
158 ms |
16572 KB |
Output is correct |
10 |
Correct |
140 ms |
21808 KB |
Output is correct |
11 |
Correct |
140 ms |
23372 KB |
Output is correct |
12 |
Correct |
156 ms |
23756 KB |
Output is correct |
13 |
Correct |
147 ms |
26288 KB |
Output is correct |
14 |
Correct |
141 ms |
21204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
17312 KB |
Output is correct |
2 |
Correct |
186 ms |
16976 KB |
Output is correct |
3 |
Correct |
169 ms |
21732 KB |
Output is correct |
4 |
Correct |
145 ms |
21392 KB |
Output is correct |
5 |
Correct |
178 ms |
17100 KB |
Output is correct |
6 |
Correct |
161 ms |
24148 KB |
Output is correct |
7 |
Correct |
123 ms |
17144 KB |
Output is correct |
8 |
Correct |
171 ms |
22672 KB |
Output is correct |
9 |
Correct |
126 ms |
22392 KB |
Output is correct |
10 |
Correct |
148 ms |
21564 KB |
Output is correct |
11 |
Correct |
135 ms |
16756 KB |
Output is correct |
12 |
Correct |
160 ms |
20600 KB |
Output is correct |
13 |
Correct |
163 ms |
22232 KB |
Output is correct |
14 |
Correct |
125 ms |
21044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
13984 KB |
Output is correct |
2 |
Correct |
101 ms |
13956 KB |
Output is correct |
3 |
Correct |
97 ms |
17740 KB |
Output is correct |
4 |
Correct |
122 ms |
18460 KB |
Output is correct |
5 |
Correct |
148 ms |
16588 KB |
Output is correct |
6 |
Correct |
131 ms |
21708 KB |
Output is correct |
7 |
Correct |
61 ms |
11044 KB |
Output is correct |
8 |
Correct |
84 ms |
15156 KB |
Output is correct |
9 |
Correct |
72 ms |
17088 KB |
Output is correct |
10 |
Correct |
79 ms |
14172 KB |
Output is correct |
11 |
Correct |
119 ms |
23204 KB |
Output is correct |
12 |
Correct |
147 ms |
20112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
133 ms |
17228 KB |
Output is correct |
10 |
Correct |
159 ms |
16964 KB |
Output is correct |
11 |
Correct |
159 ms |
20636 KB |
Output is correct |
12 |
Correct |
143 ms |
26056 KB |
Output is correct |
13 |
Correct |
149 ms |
22212 KB |
Output is correct |
14 |
Correct |
146 ms |
20596 KB |
Output is correct |
15 |
Correct |
169 ms |
17072 KB |
Output is correct |
16 |
Correct |
153 ms |
23200 KB |
Output is correct |
17 |
Correct |
158 ms |
16572 KB |
Output is correct |
18 |
Correct |
140 ms |
21808 KB |
Output is correct |
19 |
Correct |
140 ms |
23372 KB |
Output is correct |
20 |
Correct |
156 ms |
23756 KB |
Output is correct |
21 |
Correct |
147 ms |
26288 KB |
Output is correct |
22 |
Correct |
141 ms |
21204 KB |
Output is correct |
23 |
Correct |
153 ms |
17312 KB |
Output is correct |
24 |
Correct |
186 ms |
16976 KB |
Output is correct |
25 |
Correct |
169 ms |
21732 KB |
Output is correct |
26 |
Correct |
145 ms |
21392 KB |
Output is correct |
27 |
Correct |
178 ms |
17100 KB |
Output is correct |
28 |
Correct |
161 ms |
24148 KB |
Output is correct |
29 |
Correct |
123 ms |
17144 KB |
Output is correct |
30 |
Correct |
171 ms |
22672 KB |
Output is correct |
31 |
Correct |
126 ms |
22392 KB |
Output is correct |
32 |
Correct |
148 ms |
21564 KB |
Output is correct |
33 |
Correct |
135 ms |
16756 KB |
Output is correct |
34 |
Correct |
160 ms |
20600 KB |
Output is correct |
35 |
Correct |
163 ms |
22232 KB |
Output is correct |
36 |
Correct |
125 ms |
21044 KB |
Output is correct |
37 |
Correct |
82 ms |
13984 KB |
Output is correct |
38 |
Correct |
101 ms |
13956 KB |
Output is correct |
39 |
Correct |
97 ms |
17740 KB |
Output is correct |
40 |
Correct |
122 ms |
18460 KB |
Output is correct |
41 |
Correct |
148 ms |
16588 KB |
Output is correct |
42 |
Correct |
131 ms |
21708 KB |
Output is correct |
43 |
Correct |
61 ms |
11044 KB |
Output is correct |
44 |
Correct |
84 ms |
15156 KB |
Output is correct |
45 |
Correct |
72 ms |
17088 KB |
Output is correct |
46 |
Correct |
79 ms |
14172 KB |
Output is correct |
47 |
Correct |
119 ms |
23204 KB |
Output is correct |
48 |
Correct |
147 ms |
20112 KB |
Output is correct |
49 |
Correct |
122 ms |
17196 KB |
Output is correct |
50 |
Correct |
155 ms |
17048 KB |
Output is correct |
51 |
Correct |
148 ms |
23600 KB |
Output is correct |
52 |
Correct |
141 ms |
25796 KB |
Output is correct |
53 |
Correct |
111 ms |
15888 KB |
Output is correct |
54 |
Correct |
135 ms |
16076 KB |
Output is correct |
55 |
Correct |
119 ms |
21644 KB |
Output is correct |
56 |
Correct |
133 ms |
22784 KB |
Output is correct |
57 |
Correct |
145 ms |
22752 KB |
Output is correct |
58 |
Correct |
142 ms |
21820 KB |
Output is correct |
59 |
Correct |
141 ms |
22832 KB |
Output is correct |
60 |
Correct |
140 ms |
24140 KB |
Output is correct |
61 |
Correct |
135 ms |
22696 KB |
Output is correct |