Contest Duration: ~ (local time) (180 minutes)

Submission #13243750

Source Code Expand

Copy
```n,m,k=map(int,input().split())
p=list(map(int,input().split()))
edge=[[]for _ in range(n)]
for i in range(n):
if p[i]:edge[p[i]-1].append(i)
else:s=i
def bfs(v,t):
stack=[s]
d=[-1]*n
d[s]=1
x=[1]
if t==s:return[[],1]
ans=-1
for i in stack:
for j in edge[i]:
if j==t:ans=d[i]+1;continue
if j in v:continue
stack.append(j)
d[j]=d[i]+1
x.append(d[j])
return [sorted(x),ans]
v=set()
ans=[]
for i in range(m):
for j in range(n):
if j in v:continue
x,a=bfs(v,j)
if a==-1 or len(x)<m-i-1:continue
if sum(x[:m-i-1])<=k-a<=sum(x[-(m-i-1):]):
k-=a
ans.append(j+1)
break
if len(v)!=i+1:exit(print(-1))
print(*ans)```

#### Submission Info

Submission Time 2020-05-16 11:13:28+0900 F - Coins on the tree tyawanmusi Python3 (3.4.3) 0 743 Byte WA 1168 ms 3188 KB

#### Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 s1.txt, s2.txt
All 0 / 400 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt 22 ms 3064 KB
02.txt 18 ms 3064 KB
03.txt 18 ms 3064 KB
04.txt 20 ms 3064 KB
05.txt 19 ms 3188 KB
06.txt 103 ms 3064 KB
07.txt 62 ms 3064 KB
08.txt 50 ms 3064 KB
09.txt 128 ms 3064 KB
10.txt 821 ms 3064 KB
11.txt 223 ms 3064 KB
12.txt 607 ms 3064 KB
13.txt 965 ms 3064 KB
14.txt 722 ms 3064 KB
15.txt 284 ms 3064 KB
16.txt 854 ms 3188 KB
17.txt 334 ms 3064 KB
18.txt 883 ms 3188 KB
19.txt 509 ms 3188 KB
20.txt 631 ms 3188 KB
21.txt 400 ms 3064 KB
22.txt 818 ms 3188 KB
23.txt 799 ms 3188 KB
24.txt 562 ms 3188 KB
25.txt 751 ms 3188 KB
26.txt 950 ms 3188 KB
27.txt 970 ms 3188 KB
28.txt 836 ms 3064 KB
29.txt 1168 ms 3064 KB
30.txt 814 ms 3064 KB
31.txt 26 ms 3064 KB
32.txt 18 ms 3064 KB
33.txt 20 ms 3064 KB
34.txt 18 ms 3064 KB
35.txt 46 ms 3064 KB
36.txt 46 ms 3064 KB
37.txt 18 ms 3064 KB
38.txt 18 ms 3064 KB
39.txt 19 ms 3064 KB
40.txt 21 ms 3064 KB
s1.txt 17 ms 3064 KB
s2.txt 18 ms 3064 KB