Groups linked by follows

Write code · Medium · §9.1 Following relationships

There are n users on a social network, numbered from 0 to n - 1. A record [a, b] in follows means that a follows b.

No matter who follows whom, two people are linked if there is a follow record between them. People who can reach one another by going along these links all belong to one group, and a person with no follow records at all forms a group on their own.

Complete the function solution, which returns the number of groups.

Function

solution(n, follows)

Constraints

Examples

nfollowsresult
5[[0, 1], [1, 2], [3, 4]]2
7[[0, 1], [2, 3], [2, 4], [5, 6]]3

Starter code

def solution(n, follows):
    answer = 0
    return answer

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.