cs: add “547. Number of Provinces”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-01-07 21:15:19 +01:00
parent 19bc5bb150
commit 5dded1796b
Signed by: mfocko
GPG key ID: 7C47D46246790496

35
cs/number-of-provinces.cs Normal file
View file

@ -0,0 +1,35 @@
public class Solution {
private void BFS(int[][] graph, List<bool> visited, int u0) {
var q = new Queue<int>();
q.Enqueue(u0);
visited[u0] = true;
while (q.Count > 0) {
var u = q.Dequeue();
for (var v = 0; v < graph.Length; ++v) {
if (graph[u][v] == 0 || visited[v]) {
continue;
}
q.Enqueue(v);
visited[v] = true;
}
}
}
public int FindCircleNum(int[][] isConnected) {
var visited = new List<bool>(Enumerable.Repeat(false, isConnected.Length));
var count = 0;
for (var i = 0; i < isConnected.Length; ++i) {
if (visited[i]) {
continue;
}
BFS(isConnected, visited, i);
++count;
}
return count;
}
}