-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path135.py
More file actions
65 lines (52 loc) · 2.14 KB
/
135.py
File metadata and controls
65 lines (52 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution:
def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int:
# Adjacency lists for each vertex
graph = [[] for _ in range(n)]
# Map to store frequency of each unique adjacency list
component_freq = defaultdict(int)
# Initialize adjacency lists with self-loops
for vertex in range(n):
graph[vertex] = [vertex]
# Build adjacency lists from edges
for v1, v2 in edges:
graph[v1].append(v2)
graph[v2].append(v1)
# Count frequency of each unique adjacency pattern
for vertex in range(n):
neighbors = tuple(sorted(graph[vertex]))
component_freq[neighbors] += 1
# Count complete components where size equals frequency
return sum(
1
for neighbors, freq in component_freq.items()
if len(neighbors) == freq
)
class Solution2:
def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int:
# Adjacency lists for each vertex
graph = defaultdict(list)
# Build adjacency lists from edges
for v1, v2 in edges:
graph[v1].append(v2)
graph[v2].append(v1)
complete_count = 0
visited = set()
def _dfs(curr: int, info: list) -> None:
visited.add(curr)
info[0] += 1 # Increment vertex count
info[1] += len(graph[curr]) # Add edges from current vertex
# Explore unvisited neighbors
for next_vertex in graph[curr]:
if next_vertex not in visited:
_dfs(next_vertex, info)
# Process each unvisited vertex
for vertex in range(n):
if vertex in visited:
continue
# info[0] = vertices count, info[1] = total edges count
component_info = [0, 0]
_dfs(vertex, component_info)
# Check if component is complete - edges should be vertices * (vertices-1)
if component_info[0] * (component_info[0] - 1) == component_info[1]:
complete_count += 1
return complete_count