Problem library

Friend Groups

Medium
graphsunion find

There are n players, numbered 0 to n - 1. Each pair in friendships means those two players are friends. Friendship is mutual, and friends of friends end up in the same group.

Return the number of separate friend groups. A player with no friends is a group of one.

Examples

Input: n = 5, friendships = [[0,1],[1,2],[3,4]]
Output: 2
Input: n = 3, friendships = []
Output: 3