Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- sys.setrecursionlimit(60000)
- n, m = list(map(int, input().split()))
- graph = [[] for i in range(n)]
- for i in range(m):
- x, y = list(map(int, input().split()))
- graph[x - 1].append(y - 1)
- graph[y - 1].append(x - 1)
- used = [False for i in range(n)]
- ans = 0
- res = []
- def dfs(x):
- global ans, used, graph, res
- for i in range(len(graph[x])):
- if used[graph[x][i]] == False:
- used[graph[x][i]] = True
- res.append(graph[x][i])
- dfs(graph[x][i])
- return
- res_1 = []
- for i in range(n):
- if not(used[i]):
- ans += 1
- used[i] = True
- res = [i]
- dfs(i)
- res_1.append(res)
- print(ans)
- for i in range(len(res_1)):
- print(len(res_1[i]))
- res_1[i] = [j + 1 for j in res_1[i]]
- res_1[i] = [str(j) for j in res_1[i]]
- print(' '.join(res_1[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement