Sislovesme — 412.
love[1 … N] // 1‑based indexing where love[i] = j means person i loves person j .
If i, j is not mutual, at least one of the equalities love[i]=j or love[j]=i is false. Consider the iteration where i is the smaller index of the two. If love[i] ≠ j → the algorithm’s first condition ( j = love[i] ) fails. If love[i] = j but love[j] ≠ i → the second condition fails. Thus the counter is never increased for this unordered pair. ∎ Theorem After processing a test case, mutualPairs equals the total number of mutual‑love pairs in the group. 412. Sislovesme
(A classic “mutual‑love” counting problem – often seen on SPOJ, LightOJ, and other online judges) 1️⃣ Problem statement You are given a group of N people, numbered from 1 to N . Each person loves exactly one other person (possibly himself). The love‑relationships are described by an array love[1 … N] // 1‑based indexing where love[i]
def solve() -> None: data = sys.stdin.buffer.read().split() it = iter(data) t = int(next(it)) out_lines = [] for _ in range(t): n = int(next(it)) love = [0] + [int(next(it)) for _ in range(n)] # 1‑based list ans = 0 for i in range(1, n + 1): j = love[i] if i < j and love[j] == i: ans += 1 out_lines.append(str(ans)) sys.stdout.write("\n".join(out_lines)) If love[i] ≠ j → the algorithm’s first