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