UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196382#2409. 大茶馆tkswls1001594ms48060kbC++112.8kb2023-10-24 10:56:482023-10-24 12:19:51

answer

#include <bits/stdc++.h>
using namespace std;
int n, m, fa[200005], siz[200005],  t[200005], ccnt, w[200005], f[200005][21], head[200005], nxt[200005], to[200005], cnt, num[200005], d[200005], ct, tt[200005];
map<pair<int, int>, int> ma;
struct node {
	int op, p, q, ans, z;
} b[300005];
inline int finds(int p) {
	return (fa[p] == p) ? p : fa[p] = finds(fa[p]);
}
inline void add(int p, int q) {
	nxt[++ccnt] = head[p];
	to[ccnt] = q;
	head[p] = ccnt;
}
inline int lowbit(int p) {
	return p & -p;
}
inline void tadd(int p, int q) {
	while (p <= cnt) {
		t[p] += q;
		p += lowbit(p);
	}
}
inline int query(int p) {
	int u = 0;
	while (p >= 1) {
		u += t[p];
		p -= lowbit(p);
	}
	return u;
}
inline void dfs(int p, int q) {

	f[p][0] = q;
	siz[p] = 1;
	tt[p] = ++ct;
	for (int i = 1; i <= 20; i++) {
		f[p][i] = f[f[p][i - 1]][i - 1];
	}
	d[p] = d[q] + 1;
	num[p] += num[q];
	for (int i = head[p]; i; i = nxt[i]) {
		dfs(to[i], p);
		siz[p] += siz[to[i]];
	}
//	cout << p << ")))))))" << q << ' ' << siz[p] << endl;
}
inline int lca(int p, int q) {
	if (p == q) return p;
	if (d[p] < d[q]) swap(p, q);
	for (int i = 20; i >= 0; i--) {
		if (d[f[p][i]] >= d[q]) p = f[p][i];
	}
	if (p == q) return p;
	for (int i = 20; i >= 0; i--) {
		if (f[p][i] != f[q][i]) {
			p = f[p][i], q = f[q][i];
		}
	}
	return f[p][0];
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	cnt = n;
	int op, p, q;
	for (int i = 1; i <= n; i++) fa[i] = i;
	for (int i = 1; i <= m; i++) {
		cin >> b[i].op;
		if (b[i].op == 1) {
			cin >> b[i].p >> b[i].q;
			ma[make_pair(b[i].p, b[i].q)]++;
			ma[make_pair(b[i].q, b[i].p)]++;
			if (finds(b[i].p) == finds(b[i].q)) continue;
			++cnt;
			add(cnt, finds(b[i].p));
			add(cnt, finds(b[i].q));
			fa[finds(b[i].p)] = fa[finds(b[i].q)] = cnt;
			fa[cnt] = cnt;
		} else if (b[i].op == 2) {
			cin >> b[i].p;
			num[finds(b[i].p)]++;
			b[i].p = finds(b[i].p);
		} else {
			cin >> b[i].p >> b[i].q;
			b[i].z = (finds(b[i].p) == finds(b[i].q));
		}
	}
	for (int i = cnt; i >= 1; i--) {
		if (!d[i]) {
			dfs(i, 0);
//			cout << i << "%%%%%%%";
		}
	}
	for (int i = m; i >= 1; i--) {
		if (b[i].op == 1) {
			ma[make_pair(b[i].p, b[i].q)]--;
			ma[make_pair(b[i].q, b[i].p)]--;
		} else if (b[i].op == 2) {
			tadd(tt[b[i].p], -1);
			tadd(tt[b[i].p] + siz[b[i].p], 1);
//			cout << tt[b[i].p] << "^^^^" << siz[b[i].p] << endl;
		} else {
			if (b[i].z == 0) {
				b[i].ans = 0;
			} else {
				int v = lca(b[i].p, b[i].q);
//				cout << v << ' ' << num[v] << ' ' << tt[v] << " " << query(tt[v]) << "[]\n";
				b[i].ans = num[v] + query(tt[v]) + ma[make_pair(b[i].p, b[i].q)];
			}
		}
	}
	for (int i = 1; i <= m; i++) {
		if (b[i].op == 3) {
			cout << b[i].ans << "\n";
		}
	}
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 4
Accepted
time: 0ms
memory: 1312kb

input:

10 100
1 3 7
1 1 6
3 10 3
1 3 10
1 5 10
2 7
2 6
3 6 10
3 10 3
1 4 6
2 1
2 3
2 3
1 4 10
2 10
3 6 7
3 ...

output:

0
0
2
1
4
2
3
0
0
0
0
3
0
4
0
3
4
4
5
1
11
9
6
12
11
16
19
18
18
17
17
17
14
15
14
14
16
19
18
22
18...

result:

ok 43 lines

Test #2:

score: 4
Accepted
time: 0ms
memory: 1324kb

input:

100 100
1 13 17
1 81 86
3 40 3
1 93 90
1 45 20
2 67
2 56
3 16 30
3 100 73
1 74 76
2 51
2 63
2 63
1 3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 39 lines

Test #3:

score: 4
Accepted
time: 0ms
memory: 1316kb

input:

10 100
1 3 7
1 1 6
3 10 3
1 3 10
1 5 10
2 7
2 6
3 6 10
3 10 3
1 4 6
2 1
2 3
2 3
1 4 10
2 10
3 6 7
3 ...

output:

0
0
2
1
4
2
3
0
0
0
0
3
0
4
0
3
4
4
5
1
11
9
6
12
11
16
19
18
18
17
17
17
14
15
14
14
16
19
18
22
18...

result:

ok 43 lines

Test #4:

score: 4
Accepted
time: 1ms
memory: 1324kb

input:

100 100
1 13 17
1 81 86
3 40 3
1 93 90
1 45 20
2 67
2 56
3 16 30
3 100 73
1 74 76
2 51
2 63
2 63
1 3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 39 lines

Test #5:

score: 4
Accepted
time: 4ms
memory: 2292kb

input:

1000 10000
1 13 517
1 281 586
3 540 103
1 893 890
1 45 320
2 667
2 556
3 716 630
3 1000 173
1 174 27...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3341 lines

Test #6:

score: 4
Accepted
time: 8ms
memory: 2220kb

input:

500 10000
1 221 62
1 489 199
2 459
1 254 297
3 446 242
1 158 398
1 382 460
2 114
1 95 363
3 326 155
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3284 lines

Test #7:

score: 4
Accepted
time: 5ms
memory: 2308kb

input:

1000 10000
2 903
2 488
2 269
2 282
3 532 489
1 872 426
3 275 340
1 524 804
3 629 49
3 457 904
2 229
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3386 lines

Test #8:

score: 4
Accepted
time: 62ms
memory: 15356kb

input:

100000 200000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 200000 lines

Test #9:

score: 4
Accepted
time: 94ms
memory: 17308kb

input:

100000 300000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 300000 lines

Test #10:

score: 4
Accepted
time: 0ms
memory: 1316kb

input:

10 100
1 3 7
1 1 6
3 10 3
1 3 10
1 5 10
2 7
2 6
3 6 10
3 10 3
1 4 6
2 1
2 3
2 3
1 4 10
2 10
3 6 7
3 ...

output:

0
0
2
1
4
2
3
0
0
0
0
3
0
4
0
3
4
4
5
1
11
9
6
12
11
16
19
18
18
17
17
17
14
15
14
14
16
19
18
22
18...

result:

ok 43 lines

Test #11:

score: 4
Accepted
time: 1ms
memory: 1324kb

input:

100 100
1 13 17
1 81 86
3 40 3
1 93 90
1 45 20
2 67
2 56
3 16 30
3 100 73
1 74 76
2 51
2 63
2 63
1 3...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 39 lines

Test #12:

score: 4
Accepted
time: 7ms
memory: 2292kb

input:

1000 10000
1 13 517
1 281 586
3 540 103
1 893 890
1 45 320
2 667
2 556
3 716 630
3 1000 173
1 174 27...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3341 lines

Test #13:

score: 4
Accepted
time: 4ms
memory: 2216kb

input:

500 10000
1 221 62
1 489 199
2 459
1 254 297
3 446 242
1 158 398
1 382 460
2 114
1 95 363
3 326 155
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3284 lines

Test #14:

score: 4
Accepted
time: 6ms
memory: 2308kb

input:

1000 10000
2 903
2 488
2 269
2 282
3 532 489
1 872 426
3 275 340
1 524 804
3 629 49
3 457 904
2 229
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 3386 lines

Test #15:

score: 4
Accepted
time: 60ms
memory: 15356kb

input:

100000 200000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 200000 lines

Test #16:

score: 4
Accepted
time: 86ms
memory: 17308kb

input:

100000 300000
3 79013 45517
3 15281 69586
3 2540 52103
3 2893 60890
3 70045 39320
3 27667 63305
3 12...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 300000 lines

Test #17:

score: 4
Accepted
time: 142ms
memory: 31836kb

input:

100000 200000
1 1 81089
1 1 44225
2 1
1 1 96265
2 1
1 1 78155
2 1
2 1
1 1 75754
2 1
2 1
2 1
2 1
1 1 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 22336 lines

Test #18:

score: 4
Accepted
time: 220ms
memory: 17780kb

input:

2000 200000
1 1013 1517
1 1281 1586
3 540 103
1 893 890
1 45 1320
2 1667
2 556
3 1716 1630
3 2000 11...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 66340 lines

Test #19:

score: 4
Accepted
time: 243ms
memory: 19492kb

input:

10000 200000
1 9013 5517
1 5281 9586
3 2540 2103
1 2893 890
1 45 9320
2 7667
2 2556
3 3716 9630
3 80...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 66350 lines

Test #20:

score: 4
Accepted
time: 210ms
memory: 32552kb

input:

100000 200000
1 79013 45517
1 15281 69586
3 2540 52103
1 2893 60890
1 70045 39320
2 27667
2 82556
3 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 66352 lines

Test #21:

score: 4
Accepted
time: 441ms
memory: 48060kb

input:

100000 299997
1 89714 45945
2 89714
3 89714 45945
1 45945 14996
2 89714
3 14996 45945
1 14996 3009
2...

output:

2
2
4
5
1
6
6
3
5
2
7
4
3
1
14
7
7
17
13
2
18
23
12
8
1
21
6
13
17
21
5
27
5
26
2
19
3
39
40
25
22
9...

result:

ok 99999 lines

Extra Test:

score: 0
Extra Test Passed