Submission #13244728


Source Code Expand

Copy
q=range;n,m,k,*p=map(int,open(0).read().split());e=[[]for _ in q(n)]
for i in q(n):
  if p[i]:e[p[i]-1].append(i)
  else:r=i
def b(v,t):
  c=[r];d=[0]*n;d[r]=1;x=[1];a=-1
  if t==r:return[[],1]
  for i in c:
    for j in e[i]:
      if j==t:a=d[i]+1;continue
      if j not in v:c.append(j);d[j]=d[i]+1;x.append(d[j])
  return[sorted(x),a]
v=set();z=[]
for i in q(m):
  w=m-i-1
  for j in q(n+1):
    if j==n:exit(print(-1))
    x,a=b(v,j)
    if(a<1)|(len(x)<w)|(j in v):continue
    if sum(x[:w])<=k-a<=sum(x[::-1][:w]):k-=a;z.append(j+1);v.add(j);break
print(*z)

Submission Info

Submission Time
Task F - Coins on the tree
User tyawanmusi
Language PyPy3 (2.4.0)
Score 400
Code Size 585 Byte
Status
Exec Time 612 ms
Memory 48604 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 s1.txt, s2.txt
All 400 / 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 217 ms 41968 KB
02.txt 169 ms 38256 KB
03.txt 188 ms 39536 KB
04.txt 215 ms 41840 KB
05.txt 190 ms 39664 KB
06.txt 248 ms 43740 KB
07.txt 236 ms 43372 KB
08.txt 277 ms 45532 KB
09.txt 258 ms 43996 KB
10.txt 498 ms 46812 KB
11.txt 302 ms 45656 KB
12.txt 381 ms 45404 KB
13.txt 464 ms 45404 KB
14.txt 396 ms 45788 KB
15.txt 281 ms 44252 KB
16.txt 497 ms 45916 KB
17.txt 352 ms 45148 KB
18.txt 603 ms 48220 KB
19.txt 492 ms 45788 KB
20.txt 579 ms 48604 KB
21.txt 372 ms 44764 KB
22.txt 606 ms 46172 KB
23.txt 612 ms 46044 KB
24.txt 582 ms 46172 KB
25.txt 583 ms 46300 KB
26.txt 537 ms 46684 KB
27.txt 590 ms 48472 KB
28.txt 551 ms 46556 KB
29.txt 578 ms 48348 KB
30.txt 523 ms 45276 KB
31.txt 188 ms 39792 KB
32.txt 176 ms 38768 KB
33.txt 184 ms 39664 KB
34.txt 168 ms 38256 KB
35.txt 195 ms 40304 KB
36.txt 206 ms 40816 KB
37.txt 173 ms 38768 KB
38.txt 174 ms 38768 KB
39.txt 182 ms 39280 KB
40.txt 181 ms 39152 KB
s1.txt 176 ms 38256 KB
s2.txt 168 ms 38256 KB