Advertisement
DimaT1

111540

Aug 13th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #-*-coding:utf8;-*-
  2. #qpy:3
  3. #qpy:console
  4.  
  5. def dfs(g, i, c, s, used):
  6.   used[i] = True
  7.   s.append(i)
  8.   c[len(c)-1]+= 1
  9.   for j in g[i]:
  10.     if not used[j]:
  11.       dfs(g, j, c, s, used)
  12.  
  13.  
  14. def csearch(g, n):
  15.   count = 0
  16.   used = [False for i in range(n+1)]
  17.   c = []
  18.   s = []
  19.   for i in range(n):
  20.     if not used[i+1]:
  21.       count+= 1
  22.       c.append(0)
  23.       s.append([])
  24.       dfs(g, i+1, c, s[len(s)-1], used)
  25.   print(count)
  26.   for i in range(count):
  27.     print(c[i])
  28.     for j in s[i]:
  29.       print(str(j) + " ", end = "")
  30.     print()
  31.  
  32. #*****************
  33.  
  34. n, m = map(int, input().split())
  35. g = [[] for i in range(n+1)]
  36. for i in range(m):
  37.   a, b = map(int, input().split())
  38.   g[a].append(b)
  39.   g[b].append(a)
  40.  
  41. #for i in g: print(i)
  42.  
  43. csearch(g, n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement