Created
May 16, 2022 21:57
-
-
Save sunho/ae33c75614e058bb33be9a87d97ede81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.setrecursionlimit(100000) | |
n,m = input().split() | |
n = int(n) | |
m = int(m) | |
g = [[] for _ in range(n)] | |
for _ in range(m): | |
a,b = input().split() | |
a = int(a) | |
b = int(b) | |
g[a-1].append(b-1) | |
g[b-1].append(a-1) | |
vis = [False] * n | |
def dfs(u): | |
if vis[u]: | |
return | |
vis[u] = True | |
for v in g[u]: | |
dfs(v) | |
numComp = 0 | |
for i in range(n): | |
if not vis[i]: | |
numComp += 1 | |
dfs(i) | |
print(numComp - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment