Submission #3767818


Source Code Expand

from collections import defaultdict,deque
import sys
from copy import deepcopy
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=deepcopy(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 1646 Byte
Status TLE
Exec Time 2107 ms
Memory 67164 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 2107 ms 63068 KB
02.txt AC 211 ms 42220 KB
03.txt TLE 2107 ms 64220 KB
04.txt TLE 2107 ms 62428 KB
05.txt TLE 2107 ms 63836 KB
06.txt TLE 2107 ms 62940 KB
07.txt TLE 2106 ms 57716 KB
08.txt TLE 2107 ms 59612 KB
09.txt TLE 2107 ms 62940 KB
10.txt TLE 2107 ms 61276 KB
11.txt TLE 2107 ms 62684 KB
12.txt TLE 2107 ms 62684 KB
13.txt TLE 2107 ms 61916 KB
14.txt TLE 2107 ms 60636 KB
15.txt TLE 2107 ms 60892 KB
16.txt TLE 2106 ms 59016 KB
17.txt TLE 2107 ms 62428 KB
18.txt TLE 2107 ms 63964 KB
19.txt TLE 2107 ms 62940 KB
20.txt TLE 2107 ms 61148 KB
21.txt TLE 2107 ms 61660 KB
22.txt TLE 2107 ms 59996 KB
23.txt TLE 2107 ms 59996 KB
24.txt TLE 2107 ms 61916 KB
25.txt TLE 2106 ms 64444 KB
26.txt TLE 2107 ms 60252 KB
27.txt TLE 2107 ms 59100 KB
28.txt TLE 2107 ms 62940 KB
29.txt TLE 2106 ms 60988 KB
30.txt TLE 2107 ms 59996 KB
31.txt TLE 2107 ms 61788 KB
32.txt TLE 2107 ms 64860 KB
33.txt TLE 2107 ms 62812 KB
34.txt TLE 2107 ms 67164 KB
35.txt TLE 2107 ms 58460 KB
36.txt TLE 2107 ms 55900 KB
37.txt AC 312 ms 49372 KB
38.txt AC 302 ms 50524 KB
39.txt AC 271 ms 46684 KB
40.txt AC 299 ms 47068 KB
s1.txt AC 172 ms 38256 KB
s2.txt AC 172 ms 38256 KB