pastebin

Paste Search Dynamic
Recent pastes
setrecursionlimit
  1. import sys
  2.  
  3. sys.setrecursionlimit(5000)
  4. input = sys.stdin.readline
  5.  
  6.  
  7. n, m = map(int, input().split())
  8.  
  9. graph = [[] for i in range(n + 1)]
  10.  
  11. for i in range(m):
  12.         a, b = map(int, input().split())
  13.         graph[a].append(b)
  14.         graph[b].append(a)
  15.  
  16. visited = [0] * (n + 1)
  17.  
  18. cnt = 0
  19.  
  20. def dfs(graph, v, visited):
  21.         visited[v] = 1
  22.         for i in graph[v]:
  23.                 if visited[i] == 0:
  24.                         dfs(graph, i, visited)
  25.  
  26. for i in range(1, n + 1):
  27.         if not visited[i]:
  28.                 if not graph[i]:
  29.                         cnt += 1
  30.                         vistied[i] = 1
  31.                 else:
  32.                         dfs(graph, i, visited)
  33.                         cnt += 1
  34.  
  35. print(cnt)
Parsed in 0.004 seconds