init
This commit is contained in:
commit
851751ea87
82 changed files with 2093 additions and 0 deletions
24
1B.py
Normal file
24
1B.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
V, E = map(int, input().split())
|
||||
adj = [[] for i in range(V + 1)]
|
||||
for i in range(E):
|
||||
u, v = map(int, input().split())
|
||||
adj[u].append(v)
|
||||
adj[v].append(u)
|
||||
visited = [False] * (V + 1)
|
||||
ans = []
|
||||
for i in range(1, V + 1):
|
||||
if not visited[i]:
|
||||
comp = []
|
||||
stack = [i]
|
||||
visited[i] = True
|
||||
while stack:
|
||||
u = stack.pop()
|
||||
comp.append(u)
|
||||
for v in adj[u]:
|
||||
if not visited[v]:
|
||||
visited[v] = True
|
||||
stack.append(v)
|
||||
ans.append(comp)
|
||||
print(len(ans) - 1)
|
||||
for i in range(len(ans) - 1):
|
||||
print(ans[i][0], ans[i+1][0])
|
||||
Loading…
Add table
Add a link
Reference in a new issue