Algorithms Playbook
← All problems
MediumUnion-find →Similar to: Number of Provinces

Friend circles

Problem

There are n people. friends[i][j] = 1 means i and j are direct friends (the matrix is symmetric). A circle is a group where everyone is connected through chains of friends. Return the number of circles.

Examples

Input: [[1,1,0],[1,1,0],[0,0,1]]

Output: 2

Input: [[1,0,0],[0,1,0],[0,0,1]]

Output: 3

Hints

    Try it first. Every hint reveals a bit more.

    Solution