This commit is contained in:
alexey_makov 2026-07-01 19:46:51 +03:00
commit 851751ea87
82 changed files with 2093 additions and 0 deletions

24
1J.py Normal file
View file

@ -0,0 +1,24 @@
import heapq
n = int(input())
adj = [[] for _ in range(n + 1)]
in_deg = [0] * (n + 1)
for i in range(1, n + 1):
data = list(map(int, input().split()))
if data[0] > 0:
for parent in data[1:]:
adj[parent].append(i)
in_deg[i] += 1
queue = []
for i in range(1, n + 1):
if in_deg[i] == 0:
heapq.heappush(queue, i)
ans = []
while queue:
cur = heapq.heappop(queue)
ans.append(cur)
for nb in adj[cur]:
in_deg[nb] -= 1
if in_deg[nb] == 0:
heapq.heappush(queue, nb)
print(*(ans))