Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8931209

Browse files
committed
Added python solution for 323-Number-of-Connected-Components-in-an-Undirected-Graph
1 parentd030673 commit8931209

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
classUnionFind:
2+
def__init__(self):
3+
self.f= {}
4+
5+
deffindParent(self,x):
6+
y=self.f.get(x,x)
7+
ifx!=y:
8+
y=self.f[x]=self.findParent(y)
9+
returny
10+
11+
defunion(self,x,y):
12+
self.f[self.findParent(x)]=self.findParent(y)
13+
14+
classSolution:
15+
defcountComponents(self,n:int,edges:List[List[int]])->int:
16+
dsu=UnionFind()
17+
fora,binedges:
18+
dsu.union(a,b)
19+
returnlen(set(dsu.findParent(x)forxinrange(n)))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp