Cod sursa(job #2519950)

Utilizator DawlauAndrei Blahovici Dawlau Data 8 ianuarie 2020 18:18:25
Problema Parcurgere DFS - componente conexe Scor 50
Compilator py Status done
Runda Arhiva educationala Marime 0.61 kb
def DFS(node):

    seen[node] = True
    for adjNode in adjList[node]:
        if not seen[adjNode]:
            DFS(adjNode)


fin = open("dfs.in", "r")
fout = open("dfs.out", "w")

_input = fin.readline()
V, E = map(int, _input.split())

adjList = {}

for node in range(1, V + 1):
    adjList[node] = []

for edge in range(E):

    _input = fin.readline()
    x, y = map(int, _input.split())

    adjList[x].append(y)
    adjList[y].append(x)

seen = [False] * (V + 1)
CC = 0

for node in range(1, V + 1):
    if not seen[node]:
        DFS(node)
        CC += 1

fout.write(str(CC))