#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 1e9;
const ll INF = 1e18;
struct UFT {
int p[100010];
void init(int k) {
for(int i=1; i<=k; i++) p[i] = i;
}
int Find(int a) { return p[a] = p[a] == a ? a : Find(p[a]); }
void Union(int a, int b) {
p[Find(b)] = Find(a);
}
} T[3];
int n, q, cnt;
int t[300010];
int a[300010];
int b[300010];
int sp[100010][21];
int L[100010], R[100010];
int d[100010];
vector<int> g[100010];
int tree[100010];
void dfs(int x) {
L[x] = ++cnt;
for(int i=1; i<=20; i++)
sp[x][i] = sp[sp[x][i-1]][i-1];
for(auto i : g[x]) {
if(i == sp[x][0]) continue;
d[i] = d[x] + 1;
sp[i][0] = x;
dfs(i);
}
R[x] = cnt;
}
int lca(int a, int b) {
if(d[a] > d[b]) swap(a, b);
for(int i=20; i>=0; i--) {
if(d[b] - d[a] >= (1<<i)) b = sp[b][i];
}
if(a == b) return a;
for(int i=20; i>=0; i--) {
if(sp[a][i] != sp[b][i]) a = sp[a][i], b = sp[b][i];
}
return sp[a][0];
}
void update(int i, int x) {
while(i <= n+1) {
tree[i] += x;
i += i & -i;
}
}
int sum(int i) {
int ret = 0;
while(i) {
ret += tree[i];
i -= i & -i;
}
return ret;
}
int main() {
fast;
cin >> n >> q;
T[0].init(n);
for(int i=1; i<=q; i++) {
cin >> t[i] >> a[i] >> b[i];
if(t[i] == 2) continue;
if(T[0].Find(a[i]) != T[0].Find(b[i])) {
g[a[i]].eb(b[i]);
g[b[i]].eb(a[i]);
T[0].Union(a[i], b[i]);
}
}
T[0].init(n);
T[1].init(n);
for(int i=1; i<=n; i++) {
if(d[i]) continue;
d[i] = 1;
dfs(i);
}
for(int i=1; i<=n; i++) {
update(L[i], 1);
update(R[i]+1, -1);
}
for(int i=1; i<=q; i++) {
if(t[i] == 1) {
if(T[0].Find(a[i]) != T[0].Find(b[i])) {
g[a[i]].eb(b[i]);
g[b[i]].eb(a[i]);
T[0].Union(a[i], b[i]);
}
else {
a[i] = T[1].Find(a[i]);
b[i] = T[1].Find(b[i]);
while(a[i] != b[i]) {
if(d[a[i]] < d[b[i]]) swap(a[i], b[i]);
update(L[a[i]], -1);
update(R[a[i]]+1, 1);
T[1].Union(sp[a[i]][0], a[i]);
a[i] = T[1].Find(a[i]);
}
}
}
else {
if(T[0].Find(a[i]) != T[0].Find(b[i])) cout << -1 << "\n";
else cout << sum(L[a[i]]) + sum(L[b[i]]) - 2 * sum(L[lca(a[i], b[i])]) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2944 KB |
Output is correct |
2 |
Correct |
4 ms |
2944 KB |
Output is correct |
3 |
Correct |
4 ms |
2944 KB |
Output is correct |
4 |
Correct |
4 ms |
2944 KB |
Output is correct |
5 |
Correct |
4 ms |
2944 KB |
Output is correct |
6 |
Correct |
4 ms |
2944 KB |
Output is correct |
7 |
Correct |
4 ms |
2944 KB |
Output is correct |
8 |
Correct |
4 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
24176 KB |
Output is correct |
2 |
Correct |
312 ms |
24396 KB |
Output is correct |
3 |
Correct |
416 ms |
28408 KB |
Output is correct |
4 |
Correct |
453 ms |
27580 KB |
Output is correct |
5 |
Correct |
442 ms |
27236 KB |
Output is correct |
6 |
Correct |
413 ms |
27128 KB |
Output is correct |
7 |
Correct |
340 ms |
25208 KB |
Output is correct |
8 |
Correct |
452 ms |
27128 KB |
Output is correct |
9 |
Correct |
315 ms |
24824 KB |
Output is correct |
10 |
Correct |
436 ms |
26488 KB |
Output is correct |
11 |
Correct |
426 ms |
27896 KB |
Output is correct |
12 |
Correct |
378 ms |
28920 KB |
Output is correct |
13 |
Correct |
411 ms |
29048 KB |
Output is correct |
14 |
Correct |
439 ms |
27640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
277 ms |
24176 KB |
Output is correct |
2 |
Correct |
346 ms |
25080 KB |
Output is correct |
3 |
Correct |
429 ms |
27772 KB |
Output is correct |
4 |
Correct |
412 ms |
28028 KB |
Output is correct |
5 |
Correct |
301 ms |
25384 KB |
Output is correct |
6 |
Correct |
501 ms |
27896 KB |
Output is correct |
7 |
Correct |
232 ms |
25072 KB |
Output is correct |
8 |
Correct |
452 ms |
27000 KB |
Output is correct |
9 |
Correct |
324 ms |
26360 KB |
Output is correct |
10 |
Correct |
474 ms |
28428 KB |
Output is correct |
11 |
Correct |
271 ms |
24560 KB |
Output is correct |
12 |
Correct |
435 ms |
28408 KB |
Output is correct |
13 |
Correct |
474 ms |
28792 KB |
Output is correct |
14 |
Correct |
401 ms |
27772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
22384 KB |
Output is correct |
2 |
Correct |
221 ms |
22520 KB |
Output is correct |
3 |
Correct |
253 ms |
24568 KB |
Output is correct |
4 |
Correct |
224 ms |
24824 KB |
Output is correct |
5 |
Correct |
262 ms |
24824 KB |
Output is correct |
6 |
Correct |
294 ms |
28024 KB |
Output is correct |
7 |
Correct |
130 ms |
19960 KB |
Output is correct |
8 |
Correct |
170 ms |
22520 KB |
Output is correct |
9 |
Correct |
190 ms |
23304 KB |
Output is correct |
10 |
Correct |
181 ms |
22508 KB |
Output is correct |
11 |
Correct |
257 ms |
25336 KB |
Output is correct |
12 |
Correct |
297 ms |
26872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2944 KB |
Output is correct |
2 |
Correct |
4 ms |
2944 KB |
Output is correct |
3 |
Correct |
4 ms |
2944 KB |
Output is correct |
4 |
Correct |
4 ms |
2944 KB |
Output is correct |
5 |
Correct |
4 ms |
2944 KB |
Output is correct |
6 |
Correct |
4 ms |
2944 KB |
Output is correct |
7 |
Correct |
4 ms |
2944 KB |
Output is correct |
8 |
Correct |
4 ms |
2944 KB |
Output is correct |
9 |
Correct |
263 ms |
24176 KB |
Output is correct |
10 |
Correct |
312 ms |
24396 KB |
Output is correct |
11 |
Correct |
416 ms |
28408 KB |
Output is correct |
12 |
Correct |
453 ms |
27580 KB |
Output is correct |
13 |
Correct |
442 ms |
27236 KB |
Output is correct |
14 |
Correct |
413 ms |
27128 KB |
Output is correct |
15 |
Correct |
340 ms |
25208 KB |
Output is correct |
16 |
Correct |
452 ms |
27128 KB |
Output is correct |
17 |
Correct |
315 ms |
24824 KB |
Output is correct |
18 |
Correct |
436 ms |
26488 KB |
Output is correct |
19 |
Correct |
426 ms |
27896 KB |
Output is correct |
20 |
Correct |
378 ms |
28920 KB |
Output is correct |
21 |
Correct |
411 ms |
29048 KB |
Output is correct |
22 |
Correct |
439 ms |
27640 KB |
Output is correct |
23 |
Correct |
277 ms |
24176 KB |
Output is correct |
24 |
Correct |
346 ms |
25080 KB |
Output is correct |
25 |
Correct |
429 ms |
27772 KB |
Output is correct |
26 |
Correct |
412 ms |
28028 KB |
Output is correct |
27 |
Correct |
301 ms |
25384 KB |
Output is correct |
28 |
Correct |
501 ms |
27896 KB |
Output is correct |
29 |
Correct |
232 ms |
25072 KB |
Output is correct |
30 |
Correct |
452 ms |
27000 KB |
Output is correct |
31 |
Correct |
324 ms |
26360 KB |
Output is correct |
32 |
Correct |
474 ms |
28428 KB |
Output is correct |
33 |
Correct |
271 ms |
24560 KB |
Output is correct |
34 |
Correct |
435 ms |
28408 KB |
Output is correct |
35 |
Correct |
474 ms |
28792 KB |
Output is correct |
36 |
Correct |
401 ms |
27772 KB |
Output is correct |
37 |
Correct |
186 ms |
22384 KB |
Output is correct |
38 |
Correct |
221 ms |
22520 KB |
Output is correct |
39 |
Correct |
253 ms |
24568 KB |
Output is correct |
40 |
Correct |
224 ms |
24824 KB |
Output is correct |
41 |
Correct |
262 ms |
24824 KB |
Output is correct |
42 |
Correct |
294 ms |
28024 KB |
Output is correct |
43 |
Correct |
130 ms |
19960 KB |
Output is correct |
44 |
Correct |
170 ms |
22520 KB |
Output is correct |
45 |
Correct |
190 ms |
23304 KB |
Output is correct |
46 |
Correct |
181 ms |
22508 KB |
Output is correct |
47 |
Correct |
257 ms |
25336 KB |
Output is correct |
48 |
Correct |
297 ms |
26872 KB |
Output is correct |
49 |
Correct |
290 ms |
24944 KB |
Output is correct |
50 |
Correct |
306 ms |
25364 KB |
Output is correct |
51 |
Correct |
406 ms |
28116 KB |
Output is correct |
52 |
Correct |
388 ms |
28792 KB |
Output is correct |
53 |
Correct |
227 ms |
24044 KB |
Output is correct |
54 |
Correct |
266 ms |
24444 KB |
Output is correct |
55 |
Correct |
301 ms |
26872 KB |
Output is correct |
56 |
Correct |
423 ms |
27256 KB |
Output is correct |
57 |
Correct |
490 ms |
27128 KB |
Output is correct |
58 |
Correct |
434 ms |
26960 KB |
Output is correct |
59 |
Correct |
427 ms |
26232 KB |
Output is correct |
60 |
Correct |
394 ms |
26680 KB |
Output is correct |
61 |
Correct |
433 ms |
27896 KB |
Output is correct |