Submission #3767712


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
  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 1658 Byte
Status TLE
Exec Time 2107 ms
Memory 58332 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 49756 KB
02.txt AC 206 ms 42204 KB
03.txt TLE 2106 ms 49116 KB
04.txt TLE 2106 ms 49756 KB
05.txt TLE 2107 ms 50524 KB
06.txt TLE 2106 ms 54736 KB
07.txt TLE 2107 ms 52572 KB
08.txt TLE 2106 ms 50908 KB
09.txt TLE 2107 ms 50268 KB
10.txt TLE 2106 ms 57180 KB
11.txt TLE 2107 ms 53212 KB
12.txt TLE 2107 ms 55132 KB
13.txt TLE 2107 ms 57052 KB
14.txt TLE 2107 ms 56284 KB
15.txt TLE 2106 ms 55580 KB
16.txt TLE 2107 ms 56028 KB
17.txt TLE 2107 ms 58076 KB
18.txt TLE 2107 ms 56796 KB
19.txt TLE 2106 ms 56344 KB
20.txt TLE 2107 ms 56156 KB
21.txt TLE 2107 ms 55644 KB
22.txt TLE 2107 ms 55644 KB
23.txt TLE 2107 ms 56412 KB
24.txt TLE 2107 ms 57436 KB
25.txt TLE 2107 ms 58332 KB
26.txt TLE 2107 ms 56540 KB
27.txt TLE 2107 ms 55644 KB
28.txt TLE 2106 ms 56596 KB
29.txt TLE 2107 ms 56668 KB
30.txt TLE 2107 ms 56412 KB
31.txt TLE 2107 ms 51548 KB
32.txt TLE 2105 ms 49976 KB
33.txt TLE 2106 ms 48348 KB
34.txt TLE 2106 ms 47708 KB
35.txt TLE 2107 ms 52828 KB
36.txt TLE 2107 ms 57052 KB
37.txt AC 261 ms 46940 KB
38.txt AC 251 ms 45404 KB
39.txt AC 271 ms 46044 KB
40.txt AC 266 ms 45660 KB
s1.txt AC 168 ms 38256 KB
s2.txt AC 167 ms 38256 KB