Cable Union Queries Problem
Cable Union Queries Problem — ExecCode Hard DSA Practice
Solve the Cable Union Queries problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.
Problem description
n computers start disconnected. You either lay an undirected cable between u and v, or ask whether they can already talk. Union-find answers this online. If cables could also be deleted, you would process operations offline with DSU rollback on a segment tree of time. Type 0 u v unions. Type 1 u v appends 1 if they are connected else 0. Return the appended bits. You are given: n — computers ops — operations Return answers to type-1 queries. Cables persist; this version never deletes.
Examples
Input {'n': 3, 'ops': [[1, 0, 1], [0, 0, 1], [1, 0, 1], [1, 0, 2]]}; Output [0, 1, 0]. Input {'n': 1, 'ops': [[1, 0, 0]]}; Output [1]. Input {'n': 4, 'ops': [[0, 0, 1], [0, 1, 2], [1, 0, 2]]}; Output [1]
Constraints
Union is undirected A node is connected to itself
Practice Cable Union Queries free on ExecCode. Browse DSA problems, topic map, and placement guides.