Submission #3767754


Source Code Expand

from collections import defaultdict,deque
import sys
sys.setrecursionlimit(10**9)
N,M,K=map(int,input().split())
p=[int(i) for i in input().split()]
inf=float("inf")
Graph=defaultdict(set)
for i in range(N):
  if p[i]!=0:
    Graph[i].add(p[i]-1)
    Graph[p[i]-1].add(i)
  else:
    root=i
dfs=deque([root])
par=[[-1]*N for i in range(20)]
dist=[-1]*N 
dist[root]=1
while dfs:
  p=dfs.popleft()
  for i in Graph[p]:
    if dist[i]<0:
      dist[i]=dist[p]+1
      par[0][i]=p
      dfs.append(i)
      
for i in range(1,18):
  for j in range(N):
    if par[i-1][j]>0:
      par[i][j]=par[i-1][par[i-1][j]]
    
def lca(x,y):
  if dist[x]>dist[y]:
    x,y=y,x
  for i in range(18):
    if (dist[y]-dist[x])& 1<<i:
      y=par[i][y]
  if x==y:
      return y
  for i in range(17,-1,-1):
    if par[i][x]!=par[i][y]:
      x=par[i][x]
      y=par[i][y]
  return par[0][y]

C=[[] for i in range(N)]
for i in range(N):
  for j in range(N):
    if lca(i,j)==i:
      C[i].append(j)
      
def f(l,k,m):
  if m==M:
    for i in range(N):
      if dist[i]==k and l[i]:
        return [i+1]
    return -1
  else:
    for i in range(N):
      if dist[i]<=k and l[i]:
        l_next=l[::]
        for c in C[i]:
          l_next[c]=False
        if f(l_next,k-dist[i],m+1)!=-1:
          res=f(l_next,k-dist[i],m+1)
          res.append(i+1)
          return res
      else:
        for c in C[i]:
          l[c]=False
  return -1
a=f([True]*N,K,1)
if a==-1:
  print(-1)
else:
  ans=list(reversed(a))
  print(" ".join(list(map(str,ans))))

      
      
  

    
    
      
    
    
      
  
  
  
    
    
  
  
  
  
  
  

Submission Info

Submission Time
Task F - Coins on the tree
User Chanyuh
Language PyPy3 (2.4.0)
Score 0
Code Size 1717 Byte
Status TLE
Exec Time 2107 ms
Memory 58716 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 2
AC × 7
TLE × 35
Set Name Test Cases
Sample s1.txt, s2.txt
All 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 TLE 2106 ms 49436 KB
02.txt AC 209 ms 42204 KB
03.txt TLE 2106 ms 48220 KB
04.txt TLE 2106 ms 48604 KB
05.txt TLE 2107 ms 51164 KB
06.txt TLE 2107 ms 55004 KB
07.txt TLE 2106 ms 52084 KB
08.txt TLE 2107 ms 52316 KB
09.txt TLE 2107 ms 52956 KB
10.txt TLE 2107 ms 57436 KB
11.txt TLE 2106 ms 54656 KB
12.txt TLE 2107 ms 54748 KB
13.txt TLE 2107 ms 57052 KB
14.txt TLE 2107 ms 56796 KB
15.txt TLE 2107 ms 55004 KB
16.txt TLE 2107 ms 56412 KB
17.txt TLE 2107 ms 58716 KB
18.txt TLE 2107 ms 58460 KB
19.txt TLE 2107 ms 56156 KB
20.txt TLE 2106 ms 56444 KB
21.txt TLE 2107 ms 56796 KB
22.txt TLE 2107 ms 56668 KB
23.txt TLE 2107 ms 56412 KB
24.txt TLE 2106 ms 55492 KB
25.txt TLE 2107 ms 55772 KB
26.txt TLE 2107 ms 57180 KB
27.txt TLE 2107 ms 56412 KB
28.txt TLE 2107 ms 57820 KB
29.txt TLE 2106 ms 56416 KB
30.txt TLE 2107 ms 55900 KB
31.txt TLE 2107 ms 51292 KB
32.txt TLE 2106 ms 50780 KB
33.txt TLE 2106 ms 51360 KB
34.txt TLE 2106 ms 47452 KB
35.txt TLE 2107 ms 52188 KB
36.txt TLE 2107 ms 50524 KB
37.txt AC 264 ms 46940 KB
38.txt AC 240 ms 45532 KB
39.txt AC 262 ms 46300 KB
40.txt AC 263 ms 46044 KB
s1.txt AC 174 ms 38256 KB
s2.txt AC 174 ms 38256 KB