CT040

Friend Group Counter

MediumAcceptance: 0.0%

A Codetikki classroom has n students whose friendships can be represented as an adjacency list. Each student belongs to a friend group defined as the transitive closure of their friendship relations. Given a friendship adjacency list, determine the number of friend groups. Input: first line n (number of students), then n lines each starting with student id and number of friends, followed by friend ids.

Example 1:

Input: 7 0 2 1 2 1 2 0 5 2 1 0 3 1 6 4 0 5 1 1 6 1 3
Output: 3

Example 2:

Input: 5 0 1 1 1 2 0 2 2 1 1 3 0 4 0
Output: 3

Constraints:

1 <= n <= 10^5 Students labeled 0 to n-1

Tags:

disjoint-set union-find graph
Loading...
Test Cases:No test cases
No test cases available.