Submission #8678690


Source Code Expand

Copy
from collections import deque

n,m = map(int,input().split())
outs = [[] for i in range(n)]
ins = [0]*n
ans = []
for i in range(n+m-1):
    a,b = map(int,input().split())
    outs[a-1].append(b-1)
    ins[b-1] += 1
S = deque([])
for i in range(n):
    if ins[i] == 0:
        S.append([i,i])
while len(S) > 0:
    cur = S.popleft()
    ans.append(cur)
    for e in outs[cur[0]]:
        ins[e] -= 1
        if ins[e] == 0:
            S.append([e,cur[0]])
ans.sort()
for i in range(n):
  if ans[i][0] == ans[i][1]:
    print(0)
  else:
    print(ans[i][1]+1)

Submission Info

Submission Time
Task D - Restore the Tree
User Syuko4omi
Language Python3 (3.4.3)
Score 500
Code Size 584 Byte
Status
Exec Time 755 ms
Memory 30220 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 a01, a02
All 500 / 500 a01, a02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34
Case Name Status Exec Time Memory
a01 20 ms 3316 KB
a02 20 ms 3316 KB
b03 20 ms 3316 KB
b04 20 ms 3316 KB
b05 20 ms 3316 KB
b06 755 ms 30156 KB
b07 743 ms 30220 KB
b08 744 ms 30212 KB
b09 344 ms 5820 KB
b10 347 ms 5720 KB
b11 356 ms 5852 KB
b12 676 ms 27120 KB
b13 695 ms 27120 KB
b14 696 ms 27160 KB
b15 642 ms 23284 KB
b16 524 ms 16840 KB
b17 590 ms 21412 KB
b18 429 ms 12036 KB
b19 664 ms 26040 KB
b20 463 ms 14504 KB
b21 575 ms 21284 KB
b22 519 ms 17596 KB
b23 568 ms 20568 KB
b24 530 ms 18576 KB
b25 551 ms 20048 KB
b26 672 ms 26660 KB
b27 469 ms 14452 KB
b28 542 ms 19424 KB
b29 685 ms 26592 KB
b30 671 ms 26008 KB
b31 656 ms 25760 KB
b32 614 ms 23732 KB
b33 604 ms 23040 KB
b34 479 ms 14580 KB