Submission #3767727


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("a".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 WA
Exec Time 2108 ms
Memory 57948 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 1
WA × 1
AC × 1
WA × 6
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 WA 209 ms 42204 KB
03.txt TLE 2106 ms 49244 KB
04.txt TLE 2106 ms 49372 KB
05.txt TLE 2106 ms 50524 KB
06.txt TLE 2106 ms 54776 KB
07.txt TLE 2106 ms 52444 KB
08.txt TLE 2106 ms 50908 KB
09.txt TLE 2107 ms 50140 KB
10.txt TLE 2108 ms 56924 KB
11.txt TLE 2107 ms 53596 KB
12.txt TLE 2107 ms 55132 KB
13.txt TLE 2107 ms 56924 KB
14.txt TLE 2107 ms 56284 KB
15.txt TLE 2106 ms 54960 KB
16.txt TLE 2107 ms 55900 KB
17.txt TLE 2107 ms 57436 KB
18.txt TLE 2107 ms 56156 KB
19.txt TLE 2106 ms 56260 KB
20.txt TLE 2107 ms 56156 KB
21.txt TLE 2107 ms 55516 KB
22.txt TLE 2107 ms 55772 KB
23.txt TLE 2107 ms 56284 KB
24.txt TLE 2106 ms 57308 KB
25.txt TLE 2107 ms 57948 KB
26.txt TLE 2107 ms 56284 KB
27.txt TLE 2107 ms 55516 KB
28.txt TLE 2106 ms 56536 KB
29.txt TLE 2107 ms 56796 KB
30.txt TLE 2107 ms 56156 KB
31.txt TLE 2106 ms 51292 KB
32.txt TLE 2106 ms 50064 KB
33.txt TLE 2106 ms 48476 KB
34.txt TLE 2106 ms 47708 KB
35.txt TLE 2107 ms 52956 KB
36.txt TLE 2107 ms 57180 KB
37.txt WA 260 ms 46940 KB
38.txt WA 239 ms 45404 KB
39.txt WA 262 ms 46172 KB
40.txt WA 266 ms 45660 KB
s1.txt WA 173 ms 38256 KB
s2.txt AC 173 ms 38256 KB