A company has n employees, numbered 1 through n, and m collaboration links. Each link connects two employees, and collaboration is mutual. Two employees are reachable from each other if a chain of collaboration links connects them.
Count the number of unordered pairs {i, j} with i != j such that i and j are reachable from each other.
n and m.m lines: two integers u and v (1-indexed, u != v), a collaboration link.A single integer: the number of reachable unordered pairs.
Example 1
Input
5 3 1 2 2 3 4 5
Expected
4
Explanation
Group {1,2,3} contributes 3 pairs and group {4,5} contributes 1 pair, so 4 reachable pairs in total.
Example 2
Input
4 0
Expected
0
Explanation
With no links no two employees can reach each other, so there are 0 pairs.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →