n=int(input())
m=int(input())

graph=[[] for _ in range(n+1)]

for i in range(m):

    u,v=map(int,input().split())

    graph[u].append(v)
    graph[v].append(u)
 
vis = [False] * (n+1)

def dfs(cur):

    vis[cur] = True

    for next in graph[cur]:
        if vis[next] == False:
            dfs(next)
dfs(1)
print(vis.count(True)-1)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: