Cod sursa(job #2754806)
| Utilizator | Data | 26 mai 2021 16:03:48 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | py | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
VISITED = {}
EDGES = {}
RESULT = []
def dfs(x: int):
global VISITED, EDGES, RESULT
VISITED[x] = True
for y in (EDGES.get(x) or []):
if not VISITED.get(y):
dfs(y)
RESULT.append(x)
with open('sortaret.in', 'r') as f:
n, m = map(int, f.readline().split())
for i in range(m):
x, y = map(int, f.readline().split())
EDGES.setdefault(x, []).append(y)
dfs(1)
g = open('sortaret.out', 'w')
while len(RESULT) > 0:
g.write(str(RESULT.pop()) + " ")
g.close()