Submission #13243648


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)
      v.add(j)
  if len(v)!=i+1:exit(print(-1))
print(*ans)

Submission Info

Submission Time
Task F - Coins on the tree
User tyawanmusi
Language PyPy3 (2.4.0)
Score 0
Code Size 730 Byte
Status
Exec Time 886 ms
Memory 46684 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 178 ms 39280 KB
02.txt 176 ms 38896 KB
03.txt 173 ms 38512 KB
04.txt 179 ms 39280 KB
05.txt 172 ms 38512 KB
06.txt 194 ms 40304 KB
07.txt 202 ms 40048 KB
08.txt 200 ms 40304 KB
09.txt 196 ms 40688 KB
10.txt 229 ms 41200 KB
11.txt 197 ms 40724 KB
12.txt 193 ms 40432 KB
13.txt 193 ms 39920 KB
14.txt 202 ms 40944 KB
15.txt 208 ms 40944 KB
16.txt 200 ms 40688 KB
17.txt 195 ms 40176 KB
18.txt 207 ms 40816 KB
19.txt 207 ms 40816 KB
20.txt 201 ms 40176 KB
21.txt 197 ms 40176 KB
22.txt 210 ms 40816 KB
23.txt 201 ms 40560 KB
24.txt 886 ms 46684 KB
25.txt 204 ms 40816 KB
26.txt 195 ms 40304 KB
27.txt 201 ms 40688 KB
28.txt 196 ms 40816 KB
29.txt 206 ms 41072 KB
30.txt 203 ms 40048 KB
31.txt 183 ms 39792 KB
32.txt 178 ms 38768 KB
33.txt 180 ms 39664 KB
34.txt 165 ms 38256 KB
35.txt 194 ms 40304 KB
36.txt 179 ms 39280 KB
37.txt 173 ms 38640 KB
38.txt 172 ms 38512 KB
39.txt 179 ms 39280 KB
40.txt 179 ms 39152 KB
s1.txt 171 ms 38256 KB
s2.txt 165 ms 38256 KB