CODE THANKS FESTIVAL 2018(Parallel)

Submission #11198823

Source codeソースコード

import sys
input = sys.stdin.readline

N, M, K = map(int, input().split())
par = [-1] + list(map(int, input().split()))
dists = [0]*(N+1)
child = [[] for _ in range(N+1)]

for i, p in enumerate(par[1:], start=1):
    child[p].append(i)

stack = [0]
while stack:
    v = stack.pop()
    for c in child[v]:
        dists[c] = dists[v]+1
        stack.append(c)

used = [0]*(N+1)
ans = []

for m in range(M-1, -1, -1):
    for i in range(1, N+1):
        if used[i] or K < dists[i]:
            continue

        reachable = 0
        d = []
        stack = [0]

        while stack:
            v = stack.pop()
            for c in child[v]:
                if c == i:
                    reachable = 1
                elif used[c] == 0:
                    d.append(dists[c])
                    stack.append(c)

        d.sort()

        if (
            not reachable
            or sum(d[:m]) > K - dists[i]  # 残りm枚のコインを置ききれない
            or (sum(d[-m:]) if m else 0) < K - dists[i]  # 残りm枚のコインでKターン潰せない
        ):
            continue

        ans.append(i)
        used[i] = 1
        K -= dists[i]
        break
    else:
        print(-1)
        exit()

print(*ans)

Submission

Task問題 F - Coins on the tree
User nameユーザ名 testes89
Created time投稿日時
Language言語 PyPy3 (2.4.0)
Status状態 WA
Score得点 0
Source lengthソースコード長 1288 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
Sample - 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

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01.txt AC 190 ms 40048 KB
02.txt AC 169 ms 38256 KB
03.txt AC 186 ms 39152 KB
04.txt AC 204 ms 40944 KB
05.txt AC 198 ms 39536 KB
06.txt AC 232 ms 42348 KB
07.txt AC 221 ms 41712 KB
08.txt AC 208 ms 40944 KB
09.txt AC 244 ms 42608 KB
10.txt AC 372 ms 43740 KB
11.txt AC 255 ms 42716 KB
12.txt AC 362 ms 44508 KB
13.txt AC 435 ms 44892 KB
14.txt AC 387 ms 43484 KB
15.txt AC 285 ms 44780 KB
16.txt AC 401 ms 43996 KB
17.txt AC 295 ms 45676 KB
18.txt AC 389 ms 43612 KB
19.txt WA
20.txt AC 361 ms 44636 KB
21.txt AC 285 ms 42972 KB
22.txt AC 396 ms 45020 KB
23.txt AC 404 ms 46428 KB
24.txt WA
25.txt AC 376 ms 45148 KB
26.txt AC 399 ms 43228 KB
27.txt AC 414 ms 43740 KB
28.txt AC 395 ms 44524 KB
29.txt AC 460 ms 44636 KB
30.txt AC 372 ms 43868 KB
31.txt AC 194 ms 40176 KB
32.txt AC 183 ms 39280 KB
33.txt AC 187 ms 40048 KB
34.txt AC 170 ms 38256 KB
35.txt AC 200 ms 41836 KB
36.txt AC 195 ms 40048 KB
37.txt AC 168 ms 38256 KB
38.txt AC 173 ms 38768 KB
39.txt AC 169 ms 38256 KB
40.txt AC 176 ms 38768 KB
s1.txt AC 168 ms 38256 KB
s2.txt AC 167 ms 38256 KB