Cod sursa(job #2754825)
| Utilizator | Data | 26 mai 2021 16:17:16 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 80 |
| Compilator | py | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
VISITED = {}
EDGES = {}
RESULT = []
def dfs(x):
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)
for i in range(1, n+1):
if not VISITED.get(i):
dfs(i)
g = open('sortaret.out', 'w')
while len(RESULT) > 0:
g.write(str(RESULT.pop()) + " ")
g.close() 