K26
Number of Connected Components
Problem Statement & Objective
Given n nodes and a list of undirected edges, return the total number of connected components in the graph.
Function Signature (Python)
number_of_connected_components(n: int, edges: list[list[int]]) -> intInput & Output Contract
Output Rule:Bağlı bileşen sayısı (tam sayı).
System Constraints
1 <= n <= 2000
0 <= kenarlar.length <= 5000Example Execution Walkthrough
n = 5, kenarlar = [[0, 1], [1, 2], [3, 4]]
Bileşen 1: {0, 1, 2}, Bileşen 2: {3, 4} -> cevap: 2Example Test Scenarios (3)
Example 1
Input:
[5,[[0,1],[1,2],[3,4]]]Expected Output:
2Example 2: Tam Bağlı
Input:
[5,[[0,1],[1,2],[2,3],[3,4]]]Expected Output:
1Example 3: Kenarsız
Input:
[4,[]]Expected Output:
4