We are asked to write a program that manipulates a disjoint set (union-find). Input: first line n and q. Then q lines: each line has com x y, where com is 0 for unite (union) and 1 for same (find if same set). For each same operation, output 1 if x and y are in same set, else 0.
Constraints: n up to 10000, q up to 100000. x ≠ y. So we need to handle union and find efficiently.
We can use Union-Find with path compression and union by rank/size.
The output: For each same operation, print 1 or 0 on separate lines.
Edge cases: none.
We should parse input, initialize parent array from 0 to n-1, and rank/size array.
Union operation: find roots of x and y, if different, union them. Usually we union by rank: attach smaller rank tree under larger rank, if equal, increase rank.
Same operation: find roots of x and y, compare.
We'll read from stdin, output to stdout.
Implementation in Python:
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # path compression half
x = parent[x]
return x
Or recursive with path compression.
Since n is only up to 10000, recursion depth may be fine, but we can use iterative.
Union: rx, ry = find(x), find(y); if rx != ry: if rank[rx] < rank[ry]: parent[rx] = ry; elif rank[rx] > rank[ry]: parent[ry] = rx; else: parent[ry] = rx; rank[rx] += 1.
Alternatively union by size: store size.
Ok.
We need to read many lines, so use sys.stdin.read() or sys.stdin.buffer for speed.
We'll implement:
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
n = int(next(it))
q = int(next(it))
parent = list(range(n))
rank = [0]*n
def find(x):
# path compression
while parent[x] != x:
parent[x] = parent[parent[x]] # half step
x = parent[x]
return x
# or recursive:
# if parent[x] != x:
# parent[x] = find(parent[x])
# return parent[x]
out_lines = []
for _ in range(q):
com = int(next(it))
x = int(next(it))
y = int(next(it))
if com == 0:
# unite
rx = find(x)
ry = find(y)
if rx != ry:
# union by rank
if rank[rx] < rank[ry]:
parent[rx] = ry
elif rank[rx] > rank[ry]:
parent[ry] = rx
else:
parent[ry] = rx
rank[rx] += 1
else: # com == 1
rx = find(x)
ry = find(y)
out_lines.append('1' if rx == ry else '0')
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
That should work.
Test with given example.
Let's simulate:
n=5, q=12
parent = [0,1,2,3,4]
Operations:
0 1 4: unite(1,4) -> roots 1 and 4, union: rank[1]=0, rank[4]=0 -> set parent[4]=1, rank[1]=1
0 2 3: unite(2,3) -> union 2 and 3 -> parent[3]=2, rank[2]=1
1 1 2: find(1)=1, find(2)=2 -> different -> output 0
1 3 4: find(3): parent[3]=2, find(2)=2; find(4): parent[4]=1, find(1)=1 -> 2!=1 -> 0
1 1 4: find(1)=1, find(4): 4->1 -> 1 -> output 1
1 3 2: find(3)->2, find(2)=2 -> same -> 1
0 1 3: unite(1,3) -> find(1)=1, find(3)=2 -> roots 1 and 2 -> rank[1]=1, rank[2]=1: compare? rank[1]==rank[2] => parent[2]=1, rank[1] becomes 2 (since we set parent[ry]=rx, with rx=1, ry=2). So parent[2]=1.
1 2 4: find(2): parent[2]=1, find(1)=1; find(4): parent[4]=1 -> same -> 1
1 3 0: find(3): 3->2->1; find(0)=0 -> diff -> 0? Wait output example: after "1 3 0" they output 0? Actually example output lines:
0
0
1
1
1
0
1
1
Let's list expected outputs per same ops:
Same ops occur at lines 3,4,5,6,8,9,11,12 (the ones with com=1). Counting:
- com1 1 2 -> output 0
- com2 3 4 -> output 0
- com3 1 4 -> output 1
- com4 3 2 -> output 1
- com5 2 4 -> after unite 1 3, output 1
- com6 3 0 -> output 0
- com7 0 2 -> after unite 0 4, output 1
- com8 3 0 -> output 1? Wait the last two lines: after "0 0 4" then "1 0 2" and "1 3 0". According to example: Output has 8 lines. Let's map:
Input lines:
0 1 4 (unite)
0 2 3 (unite)
1 1 2 (same) -> first output 0
1 3 4 (same) -> second output 0
1 1 4 (same) -> third output 1
1 3 2 (same) -> fourth output 1
0 1 3 (unite)
1 2 4 (same) -> fifth output 1
1 3 0 (same) -> sixth output 0
0 0 4 (unite)
1 0 2 (same) -> seventh output 1
1 3 0 (same) -> eighth output 1
Thus for "1 3 0" first time (before last unite) output 0; second time after unite 0 4 (and all unions) output 1.
Our code will produce correct if union operations propagate correctly.
After unite(1,4) and unite(2,3) and unite(1,3) we have all 1,2,3,4 in same set with root 1. Then unite(0,4) after that? Actually after unite(1,3) we have set {0} separate, {1,2,3,4} together. Then we have same(2,4) -> 1, same(3,0) -> 0. Then unite(0,4) -> find(0)=0, find(4)=1 -> union: rank[0]=0, rank[1]=2? Actually rank[1] became 2 after we merged 1 and 2? Let's recalc: after first union: rank[1]=1 (since parent[4]=1). After second union: rank[2]=1 (parent[3]=2). After third union (1 and 2): rx=1 rank 1, ry=2 rank 1 -> we set parent[2]=1 and rank[1] becomes 2. So rank[1]=2. So when we unite 0 (rank[0]=0) with 1 (rank[1]=2), we will attach 0 under 1 (since rank[0] < rank[1]). So parent[0]=1. Then same(0,2): find(0)->1, find(2)->1 -> 1; same(3,0): find(3)->1, find(0)->1 ->1. So output matches.
Thus code works.
Potential issues: Input values may be separated by spaces and newlines. Our reading with split() works.
Complexities: O(q α(n)). Good.
Now produce final answer: Explanation and code.
We should provide a description and the program. The problem is straightforward.
We need to output only the code? The instruction: "Write a program ..." So likely just code.
But the prompt says: "Write a program which manipulates a disjoint set..." So we should output the program code.
Thus final answer: Python code.
Now produce final. ```python
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
n = int(next(it))
q = int(next(it))
parent = list(range(n))
rank = [0] * n
# find with path compression (iterative)
def find(x):
while parent[x] != x:
# halve the path
parent[x] = parent[parent[x]]
x = parent[x]
return x
out = []
for _ in range(q):
com = int(next(it))
x = int(next(it))
y = int(next(it))
if com == 0: # unite
rx, ry = find(x), find(y)
if rx != ry:
# union by rank
if rank[rx] < rank[ry]:
parent[rx] = ry
elif rank[rx] > rank[ry]:
parent[ry] = rx
else:
parent[ry] = rx
rank[rx] += 1
else: # same
out.append('1' if find(x) == find(y) else '0')
sys.stdout.write("\n".join(out))
if name == "main":
main()