Bear and Friendship Condition
MediumRating 1500CodeforcesUnion-Find (DSU)
Approach hintTry it first, then revealHide
The condition holds exactly when every connected component is a clique. Track vertex and edge counts per component in a DSU and check that s vertices come with s * (s - 1) / 2 edges, using 64-bit values.