Write a program which reads relations in a SNS (Social Network Service), and judges that…
- User
Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.
Constraints
- Input
Assistant
pythonn, m = map(int, input().split()) parent = list(range(n)) rank = [1] * n def find(u): while parent[u] != u: parent[u] = parent[parent[u]] u = parent[u] return u def union(u, v): u_root = find(u) v_root = find(v) if u_root == v_root: return if rank[u_root] < rank[v_root]: parent[u_root] = v_root else: parent[v_root] = u_root if rank[u_root] == rank[v_root]: rank[u_root] += 1 for _ in range(m): s, t = map(int, input().split()) union(s, t) q = int(input()) for _ in range(q): s, t = map(int, input().split()) print("yes" if find(s) == find(t) else "no")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.