Uploaded by Paulina SiemiΕ„ska

Disjoint Sets na poniedzialek– Kattis, IT-Universitetet i København

advertisement
07/02/2021
Disjoint Sets – Kattis, IT-Universitetet i København
Problem ID: itu.disjointsets
Disjoint Sets
CPU Time limit: 5 seconds
Memory limit: 1024 MB
Language: da, en
Maintain a family of disjoint sets, initially the singletons {0}, {1}, … , {𝑛 − 1} , under the following operations:
0 (“query”) The query operation takes two elements 𝑠 and 𝑑 and determines if 𝑠 and 𝑑 belong to the same set. More precisely, if
𝑠 ∈ 𝑆
and 𝑑
∈ 𝑇
then print “1” if 𝑆
= 𝑇
and print “0” if 𝑆
1 (“union”) The union operation takes two elements
precisely, if 𝑠
𝑆 = 𝑇
∈ 𝑆
and 𝑑
∈ 𝑇
with 𝑆
≠ 𝑇
𝑠
and
𝑑
≠ 𝑇.
and creates the union of the two sets containing
𝑠
and 𝑑 . More
then 𝑆 and 𝑇 are removed from the family and replaced by the set 𝑆
∪ 𝑇
Author: Thore Husfeldt
Source: ITU BADS
License:
. (If
then nothing happens.)
2 (“move”) (Test groups 2 and 4 only.) The move operation takes two elements
containing the element 𝑑 . More precisely, if
changed to 𝑇
∪ {𝑠}.
(If 𝑆
= 𝑇
𝑠 ∈ 𝑆
and
𝑑 ∈ 𝑇
with
𝑠
and
𝑆 ≠ 𝑇
𝑑
and moves the element
then
𝑆
is changed to
𝑠
into the set
𝑆 − {𝑠}
and
𝑇
is
then nothing happens.)
Note that these operations maintain invariantly that the sets in the family are disjoint.
Test groups
There are 4 di erent test groups.
Test group
Points
Problem size bounds
Operations
1
10
𝑛, π‘š ≤ 1 000
0 and 1
2
10
𝑛, π‘š ≤ 1 000
0, 1, and 2
3
40
𝑛, π‘š ≤ 100 000
0 and 1
4
40
𝑛, π‘š ≤ 100 000
0, 1, and 2
Any solution to test group 3 also solves test group 1. Any solution to test group 4 solves all the other test groups.
Input
The input starts with two positive integers
𝑛, π‘š,
on the
rst line. The integer
𝑛
is the initial number of singletons. The
following π‘š lines are of the form “0 𝑠 𝑑 ” or “1 𝑠 𝑑 ” or “2 𝑠 𝑑 ”, representing query, union, and move. In test groups 1 and 3, move
never occurs. You can assume 0
≤ 𝑠 < 𝑛, 0 ≤ 𝑑 < 𝑛
and π‘š
≥ 1.
Output
For each query, write a single line containing 1 or 0 as described above.
Sample Input 1
4
0
1
0
1
0
0
1
0
0
9
0
0
0
1
1
0
2
2
0
1
1
1
2
2
3
3
3
3
https://itu.kattis.com/problems/itu.disjointsets
Sample Output 1
0
1
1
0
1
1
1/2
07/02/2021
Sample Input 2
4
0
1
0
1
0
0
2
0
0
9
0
0
0
1
1
0
2
0
1
1
1
1
2
2
3
3
1
2
Sample Input 3
2 2
1 0 0
0 0 1
https://itu.kattis.com/problems/itu.disjointsets
Disjoint Sets – Kattis, IT-Universitetet i København
Sample Output 2
0
1
1
0
1
0
Sample Output 3
0
2/2
Download