n servers numbered from 0 to n − 1. The n × n matrix links describes the cables: links[i][j] = 1 if servers i and j are wired directly, and 0 otherwise. Every link works both ways, and each server counts as linked to itself.
Servers that can reach each other through cables (directly or via other servers) belong to the same cluster. Return the number of clusters.links=[[1, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 1]] → 3
Servers 0 and 3 share a cable and form one cluster, while servers 1 and 2 stand alone. That makes three clusters.
links=[[1, 1, 0], [1, 1, 1], [0, 1, 1]] → 1
Server 0 is not wired to server 2 directly, but both are wired to server 1, so the whole network is one cluster.
links=[[1]] → 1
links is an n × n matrix of 0s and 1slinks[i][i] = 1links[i][j] = links[j][i]links=[[1, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 1]]
3
Run checks the first 3 tests in your browser. Submit checks all 60 tests on the server, hidden ones included.