CODE THANKS FESTIVAL 2018(Parallel)

Submission #11198977

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 len(d) < m  # 残り頂点数が足りない
            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状態 AC
Score得点 400
Source lengthソースコード長 1349 Byte
File nameファイル名
Exec time実行時間 444 ms
Memory usageメモリ使用量 46044 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - s1.txt,s2.txt
All 400 / 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 201 ms 41840 KB
02.txt AC 160 ms 38256 KB
03.txt AC 171 ms 39152 KB
04.txt AC 185 ms 40560 KB
05.txt AC 174 ms 39536 KB
06.txt AC 228 ms 42988 KB
07.txt AC 214 ms 41712 KB
08.txt AC 196 ms 40944 KB
09.txt AC 238 ms 42736 KB
10.txt AC 364 ms 43868 KB
11.txt AC 253 ms 42716 KB
12.txt AC 345 ms 44508 KB
13.txt AC 432 ms 44764 KB
14.txt AC 366 ms 43612 KB
15.txt AC 275 ms 44908 KB
16.txt AC 382 ms 43740 KB
17.txt AC 269 ms 45292 KB
18.txt AC 372 ms 43740 KB
19.txt AC 288 ms 42460 KB
20.txt AC 329 ms 44268 KB
21.txt AC 266 ms 43100 KB
22.txt AC 377 ms 44380 KB
23.txt AC 387 ms 46044 KB
24.txt AC 318 ms 44124 KB
25.txt AC 357 ms 44380 KB
26.txt AC 385 ms 43228 KB
27.txt AC 401 ms 43740 KB
28.txt AC 378 ms 44652 KB
29.txt AC 444 ms 44636 KB
30.txt AC 358 ms 43996 KB
31.txt AC 182 ms 40048 KB
32.txt AC 172 ms 39280 KB
33.txt AC 179 ms 40048 KB
34.txt AC 162 ms 38256 KB
35.txt AC 197 ms 41836 KB
36.txt AC 189 ms 40176 KB
37.txt AC 160 ms 38256 KB
38.txt AC 169 ms 38768 KB
39.txt AC 162 ms 38256 KB
40.txt AC 168 ms 38768 KB
s1.txt AC 159 ms 38256 KB
s2.txt AC 160 ms 38256 KB