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
2≤n≤100,000
0≤m≤100,000
Input
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In the first line, two integer n and m are given. n is the number of users in the SNS and m is the number of relations in the SNS. The users in the SNS are identified by IDs 0,1,...,n−1.
In the following m lines, the relations are given. Each relation is given by two integers s and t that represents s and t are friends (and reachable each other).
In the next line, the number of queries q is given. In the following q lines, q queries are given respectively. Each query consists of two integers s and t separated by a space character.
Output
For each query, print "yes" if t is reachable from s through the social network, "no" otherwise.
We need to read n, m, then m edges (undirected). Then q queries: check if two nodes are connected in the graph (reachable). Constraints: n up to 100k, m up to 100k, q up to 10k. So we need to process connectivity queries offline. Could use Union-Find (Disjoint Set Union) to precompute connected components. Then each query is O(1) (find root). Straightforward.
But note: the problem statement is standard.
Implementation: read n, m; initialize DSU of size n; for each edge, union(s, t). Then read q; for each query, check if find(s) == find(t); print "yes" or "no".
We must ensure fast I/O for large input? n,m up to 100k, q up to 10k, so not huge, but typical Python can handle. But reading 200k lines might be okay. However we can use sys.stdin.read to read all at once.
Edge cases: nodes may not be present? All ids 0..n-1. So fine.
Output: each answer on separate line.
I'll implement DSU with path compression and union by rank.
Potential pitfall: Input format: after m lines, next line is q, then q lines of queries. So typical.
Now, we need to output exactly "yes" and "no" lowercase.
Implementation:
import sys
def main():
data = sys.stdin.read().split()
it = iter(data)
n = int(next(it))
m = int(next(it))
parent = list(range(n))
rank = [0]*n
code
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # path compression
x = parent[x]
return x
# or recursive: if parent[x]!=x: parent[x]=find(parent[x]); return parent[x]
def union(x, y):
rx = find(x)
ry = find(y)
if rx == ry:
return
if rank[rx] < rank[ry]:
parent[rx] = ry
elif rank[rx] > rank[ry]:
parent[ry] = rx
else:
parent[ry] = rx
rank[rx] += 1
for _ in range(m):
s = int(next(it))
t = int(next(it))
union(s, t)
q = int(next(it))
out_lines = []
for _ in range(q):
s = int(next(it))
t = int(next(it))
if find(s) == find(t):
out_lines.append("yes")
else:
out_lines.append("no")
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
That's it.
But note: Could be that m = 0, then no union calls. Fine.
Also q = 0? But constraints say 1 ≤ q, so at least 1 query.
Make sure we handle all input lines.
Also, we used iterative reading, which is fine.
Now, the problem is to "Write a program" in typical programming language? Probably any language is fine? The user didn't specify language; but we need to provide answer: I think we should output a solution in Python? The instruction: "Write a program which reads relations..." Usually in the contest they expect C/C++/Java etc. But we can answer with Python code. Since it's text, we provide solution in Python. If needed, we can provide explanation. But typical "write a program" means code.
Thus answer: Provide Python code as above.
But we should also add comment to explain? Not necessary.
Potential improvements: Use sys.setrecursionlimit if recursive, but iterative find is okay.
Thus final answer.
We'll provide the code block. ```python
import sys
def main():
data = sys.stdin.read().split()
it = iter(data)
n = int(next(it))
m = int(next(it))
code
# DSU structures
parent = list(range(n))
rank = [0] * n
def find(x):
# Path compression (iterative)
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(x, y):
rx = find(x)
ry = find(y)
if rx == ry:
return
# 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
# Process edges
for _ in range(m):
s = int(next(it))
t = int(next(it))
union(s, t)
# Process queries
q = int(next(it))
out = []
for _ in range(q):
s = int(next(it))
t = int(next(it))
out.append("yes" if find(s) == find(t) else "no")
sys.stdout.write("\n".join(out))