#include <iostream>
#include <cmath>
#include <functional>
#include <vector>
using namespace std;
struct Query
{
int t, a, b;
};
Query qr[300005];
struct UnionFind
{
vector<int> par;
UnionFind(int N)
{
par.resize(N+1);
for (int i = 1; i <= N; i++) par[i] = i;
}
int fin(int v)
{
return v == par[v] ? v : (par[v] = fin(par[v]));
}
void uni(int u, int v)
{
par[fin(u)] = fin(v);
}
bool isuni(int u, int v)
{
return fin(u) == fin(v);
}
};
struct LazySeg
{
vector<int> tree, lazy;
LazySeg(int N)
{
int sz = 1 << ((int)ceil(log2(N+1))+1);
tree.resize(sz); lazy.resize(sz);
}
void propagate(int s, int e, int node)
{
if (lazy[node] == 0) return;
tree[node] = e - s + 1;
if (s != e) {
lazy[2*node] = lazy[2*node+1] = 1;
}
lazy[node] = 0;
}
void upd(int s, int e, int node, int l, int r)
{
propagate(s, e, node);
if (e < l || r < s) return;
if (l <= s && e <= r){
lazy[node] = 1;
propagate(s, e, node);
return;
}
upd(s, (s+e)/2, 2*node, l, r);
upd((s+e)/2+1, e, 2*node+1, l, r);
tree[node] = tree[2*node] + tree[2*node+1];
}
int query(int s, int e, int node, int l, int r)
{
propagate(s, e, node);
if (e < l || r < s) return 0;
if (l <= s && e <= r) return tree[node];
return query(s, (s+e)/2, 2*node, l, r) + query((s+e)/2+1, e, 2*node+1, l, r);
}
};
vector<int> adj[100005];
vector<int> g[100005];
int in[100005], out[100005], sz[100005], par[100005], dep[100005], nxt[100005];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int N, Q; cin >> N >> Q;
for (int i = 0; i < Q; i++) {
cin >> qr[i].t >> qr[i].a >> qr[i].b;
}
UnionFind uf(N);
for (int i = 0; i < Q; i++) {
if (qr[i].t == 1) {
if (!uf.isuni(qr[i].a, qr[i].b)) {
adj[qr[i].a].push_back(qr[i].b);
adj[qr[i].b].push_back(qr[i].a);
uf.uni(qr[i].a, qr[i].b);
}
}
}
for (int i = 2; i <= N; i++) {
if (!uf.isuni(1, i)) {
uf.uni(1, i);
adj[1].push_back(i);
adj[i].push_back(1);
}
}
LazySeg seg(N);
function<void(int, int)> dfs_sz = [&](int v, int p)
{
dep[v] = dep[p] + 1;
par[v] = p;
sz[v] = 1;
for (int i:adj[v]) {
if (i != p) {
g[v].push_back(i);
dfs_sz(i, v);
if (sz[g[v][0]] < sz[i]) {
swap(g[v][0], g[v].back());
}
sz[v] += sz[i];
}
}
};
int ord = 0;
function<void(int)> dfs_hld = [&](int v)
{
in[v] = ord++;
for (int i:g[v]) {
nxt[i] = g[v][0] == i ? nxt[v] : i;
dfs_hld(i);
}
out[v] = ord;
};
function<int(int, int)> lca = [&](int u, int v)
{
while (nxt[u] != nxt[v]) {
if (dep[nxt[u]] < dep[nxt[v]]) swap(u, v);
u = par[nxt[u]];
}
return dep[u] < dep[v] ? u : v;
};
function<void(int, int)> upd = [&](int u, int v)
{
int w = lca(u, v);
while (1) {
if (nxt[u] != nxt[w]) {
seg.upd(0, N-1, 1, in[nxt[u]], in[u]);
u = par[nxt[u]];
}
else {
seg.upd(0, N-1, 1, in[w]+1, in[u]);
break;
}
}
while (1) {
if (nxt[v] != nxt[w]) {
seg.upd(0, N-1, 1, in[nxt[v]], in[v]);
v = par[nxt[v]];
}
else {
seg.upd(0, N-1, 1, in[w]+1, in[v]);
break;
}
}
};
function<int(int, int)> query = [&](int u, int v)
{
int ans = 0;
int w = lca(u, v);
while (1) {
if (nxt[u] != nxt[w]) {
ans += seg.query(0, N-1, 1, in[nxt[u]], in[u]);
u = par[nxt[u]];
}
else {
ans += seg.query(0, N-1, 1, in[w]+1, in[u]);
break;
}
}
while (1) {
if (nxt[v] != nxt[w]) {
ans += seg.query(0, N-1, 1, in[nxt[v]], in[v]);
v = par[nxt[v]];
}
else {
ans += seg.query(0, N-1, 1, in[w]+1, in[v]);
break;
}
}
return ans;
};
function<int(int, int)> dis = [&](int u, int v)
{
int w = lca(u, v);
return dep[u] + dep[v] - 2 * dep[w];
};
dfs_sz(1, 0);
dfs_hld(1);
UnionFind uf2(N);
for (int i = 0; i < Q; i++) {
if (qr[i].t == 1) {
if (uf2.isuni(qr[i].a, qr[i].b)) {
upd(qr[i].a, qr[i].b);
}
else {
uf2.uni(qr[i].a, qr[i].b);
}
}
else {
if (uf2.isuni(qr[i].a, qr[i].b)) {
cout << dis(qr[i].a, qr[i].b) - query(qr[i].a, qr[i].b) << '\n';
}
else {
cout << -1 << '\n';
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5172 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
5 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
18236 KB |
Output is correct |
2 |
Correct |
470 ms |
19008 KB |
Output is correct |
3 |
Correct |
252 ms |
27972 KB |
Output is correct |
4 |
Correct |
249 ms |
28800 KB |
Output is correct |
5 |
Correct |
245 ms |
25400 KB |
Output is correct |
6 |
Correct |
226 ms |
26028 KB |
Output is correct |
7 |
Correct |
388 ms |
19308 KB |
Output is correct |
8 |
Correct |
234 ms |
25300 KB |
Output is correct |
9 |
Correct |
380 ms |
19132 KB |
Output is correct |
10 |
Correct |
229 ms |
24224 KB |
Output is correct |
11 |
Correct |
216 ms |
27168 KB |
Output is correct |
12 |
Correct |
207 ms |
31256 KB |
Output is correct |
13 |
Correct |
189 ms |
31932 KB |
Output is correct |
14 |
Correct |
217 ms |
26572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
18200 KB |
Output is correct |
2 |
Correct |
361 ms |
19036 KB |
Output is correct |
3 |
Correct |
245 ms |
26096 KB |
Output is correct |
4 |
Correct |
230 ms |
29760 KB |
Output is correct |
5 |
Correct |
203 ms |
19324 KB |
Output is correct |
6 |
Correct |
244 ms |
25744 KB |
Output is correct |
7 |
Correct |
195 ms |
18348 KB |
Output is correct |
8 |
Correct |
246 ms |
24608 KB |
Output is correct |
9 |
Correct |
222 ms |
25988 KB |
Output is correct |
10 |
Correct |
244 ms |
27340 KB |
Output is correct |
11 |
Correct |
189 ms |
18328 KB |
Output is correct |
12 |
Correct |
274 ms |
27500 KB |
Output is correct |
13 |
Correct |
230 ms |
27980 KB |
Output is correct |
14 |
Correct |
215 ms |
27600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
16872 KB |
Output is correct |
2 |
Correct |
277 ms |
17612 KB |
Output is correct |
3 |
Correct |
200 ms |
24168 KB |
Output is correct |
4 |
Correct |
167 ms |
27176 KB |
Output is correct |
5 |
Correct |
464 ms |
18992 KB |
Output is correct |
6 |
Correct |
259 ms |
27724 KB |
Output is correct |
7 |
Correct |
50 ms |
16660 KB |
Output is correct |
8 |
Correct |
64 ms |
23884 KB |
Output is correct |
9 |
Correct |
92 ms |
27800 KB |
Output is correct |
10 |
Correct |
111 ms |
17132 KB |
Output is correct |
11 |
Correct |
212 ms |
25932 KB |
Output is correct |
12 |
Correct |
276 ms |
25172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5172 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
5 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
211 ms |
18236 KB |
Output is correct |
10 |
Correct |
470 ms |
19008 KB |
Output is correct |
11 |
Correct |
252 ms |
27972 KB |
Output is correct |
12 |
Correct |
249 ms |
28800 KB |
Output is correct |
13 |
Correct |
245 ms |
25400 KB |
Output is correct |
14 |
Correct |
226 ms |
26028 KB |
Output is correct |
15 |
Correct |
388 ms |
19308 KB |
Output is correct |
16 |
Correct |
234 ms |
25300 KB |
Output is correct |
17 |
Correct |
380 ms |
19132 KB |
Output is correct |
18 |
Correct |
229 ms |
24224 KB |
Output is correct |
19 |
Correct |
216 ms |
27168 KB |
Output is correct |
20 |
Correct |
207 ms |
31256 KB |
Output is correct |
21 |
Correct |
189 ms |
31932 KB |
Output is correct |
22 |
Correct |
217 ms |
26572 KB |
Output is correct |
23 |
Correct |
240 ms |
18200 KB |
Output is correct |
24 |
Correct |
361 ms |
19036 KB |
Output is correct |
25 |
Correct |
245 ms |
26096 KB |
Output is correct |
26 |
Correct |
230 ms |
29760 KB |
Output is correct |
27 |
Correct |
203 ms |
19324 KB |
Output is correct |
28 |
Correct |
244 ms |
25744 KB |
Output is correct |
29 |
Correct |
195 ms |
18348 KB |
Output is correct |
30 |
Correct |
246 ms |
24608 KB |
Output is correct |
31 |
Correct |
222 ms |
25988 KB |
Output is correct |
32 |
Correct |
244 ms |
27340 KB |
Output is correct |
33 |
Correct |
189 ms |
18328 KB |
Output is correct |
34 |
Correct |
274 ms |
27500 KB |
Output is correct |
35 |
Correct |
230 ms |
27980 KB |
Output is correct |
36 |
Correct |
215 ms |
27600 KB |
Output is correct |
37 |
Correct |
130 ms |
16872 KB |
Output is correct |
38 |
Correct |
277 ms |
17612 KB |
Output is correct |
39 |
Correct |
200 ms |
24168 KB |
Output is correct |
40 |
Correct |
167 ms |
27176 KB |
Output is correct |
41 |
Correct |
464 ms |
18992 KB |
Output is correct |
42 |
Correct |
259 ms |
27724 KB |
Output is correct |
43 |
Correct |
50 ms |
16660 KB |
Output is correct |
44 |
Correct |
64 ms |
23884 KB |
Output is correct |
45 |
Correct |
92 ms |
27800 KB |
Output is correct |
46 |
Correct |
111 ms |
17132 KB |
Output is correct |
47 |
Correct |
212 ms |
25932 KB |
Output is correct |
48 |
Correct |
276 ms |
25172 KB |
Output is correct |
49 |
Correct |
236 ms |
18504 KB |
Output is correct |
50 |
Correct |
383 ms |
19404 KB |
Output is correct |
51 |
Correct |
262 ms |
27468 KB |
Output is correct |
52 |
Correct |
244 ms |
31372 KB |
Output is correct |
53 |
Correct |
180 ms |
17840 KB |
Output is correct |
54 |
Correct |
189 ms |
19020 KB |
Output is correct |
55 |
Correct |
259 ms |
26760 KB |
Output is correct |
56 |
Correct |
198 ms |
29612 KB |
Output is correct |
57 |
Correct |
260 ms |
25084 KB |
Output is correct |
58 |
Correct |
223 ms |
25404 KB |
Output is correct |
59 |
Correct |
196 ms |
26956 KB |
Output is correct |
60 |
Correct |
187 ms |
28772 KB |
Output is correct |
61 |
Correct |
204 ms |
27676 KB |
Output is correct |