Submission #2708411


Source Code Expand

# seishin.py
from collections import deque
N = int(input())
*P, = map(int, input().split())
MOD = 10**9 + 7

G = [[] for i in range(N+1)]
for i, p in enumerate(P):
    G[p].append(i+1)
Q = [None]*(N+1)
for i in range(N, -1, -1):
    # i+1 <- P[i]
    Q[i] = q = deque([[1, 1, 0, 1]])

    # 子ノードのdequeを集める
    R = []
    for j in G[i]:
        R.append(Q[j])

    if R:
        R.sort(key=len, reverse=1)

        # dequeの小さい方から大きい方へマージする処理
        last = R.pop()
        while R:
            nxt = R.pop()
            for k in range(len(last)):
                a0, a1, a2, c0 = last.popleft()
                b0, b1, b2, c1 = nxt[k]
                nxt[k] = [a0*b0 % MOD, (a0*b1 + a1*b0) % MOD, (a0*b2 + a1*b1 + a2*b0 + a1*b2 + a2*b2 + a2*b1) % MOD, c0+c1]
            last = nxt
        # a0 <- a2
        for e in last:
            e[0] += e[2]
            e[2] = 0
        q.extend(last)
ans = 0
for a0, a1, a2, c in Q[0]:
    ans += pow(2, N+1-c, MOD) * a1 % MOD
print(ans % MOD)

Submission Info

Submission Time
Task E - Smuggling Marbles
User yaketake08
Language PyPy3 (2.4.0)
Score 400
Code Size 1079 Byte
Status TLE
Exec Time 3190 ms
Memory 559032 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 400 / 400 0 / 600
Status
AC × 3
AC × 20
AC × 42
TLE × 10
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt, s1_14.txt, s1_15.txt, s1_16.txt, s1_17.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt, s1_14.txt, s1_15.txt, s1_16.txt, s1_17.txt, s2_01.txt, s2_02.txt, s2_03.txt, s2_04.txt, s2_05.txt, s2_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s2_15.txt, s2_16.txt, s2_17.txt, s2_18.txt, s2_19.txt, s2_20.txt, s2_21.txt, s2_22.txt, s2_23.txt, s2_24.txt, s2_25.txt, s2_26.txt, s2_27.txt, s2_28.txt, s2_29.txt, s2_30.txt, s2_31.txt, s2_32.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 165 ms 38512 KB
00_example_02.txt AC 163 ms 38256 KB
00_example_03.txt AC 163 ms 38256 KB
s1_01.txt AC 168 ms 38256 KB
s1_02.txt AC 180 ms 39664 KB
s1_03.txt AC 193 ms 42096 KB
s1_04.txt AC 163 ms 38256 KB
s1_05.txt AC 174 ms 39408 KB
s1_06.txt AC 190 ms 42224 KB
s1_07.txt AC 166 ms 38256 KB
s1_08.txt AC 273 ms 61148 KB
s1_09.txt AC 180 ms 39792 KB
s1_10.txt AC 202 ms 43120 KB
s1_11.txt AC 167 ms 38256 KB
s1_12.txt AC 183 ms 40688 KB
s1_13.txt AC 163 ms 38256 KB
s1_14.txt AC 213 ms 44396 KB
s1_15.txt AC 210 ms 43760 KB
s1_16.txt AC 207 ms 43884 KB
s1_17.txt AC 215 ms 44268 KB
s2_01.txt AC 323 ms 64368 KB
s2_02.txt AC 231 ms 46832 KB
s2_03.txt AC 769 ms 141064 KB
s2_04.txt AC 555 ms 126460 KB
s2_05.txt AC 737 ms 146592 KB
s2_06.txt AC 1025 ms 185324 KB
s2_07.txt AC 727 ms 158372 KB
s2_08.txt AC 465 ms 102460 KB
s2_09.txt AC 928 ms 177928 KB
s2_10.txt AC 773 ms 156368 KB
s2_11.txt AC 582 ms 197320 KB
s2_12.txt AC 191 ms 43248 KB
s2_13.txt TLE 3189 ms 555448 KB
s2_14.txt TLE 3189 ms 547896 KB
s2_15.txt TLE 3188 ms 545196 KB
s2_16.txt AC 254 ms 56688 KB
s2_17.txt AC 226 ms 46320 KB
s2_18.txt AC 510 ms 141064 KB
s2_19.txt AC 381 ms 117132 KB
s2_20.txt AC 1128 ms 197420 KB
s2_21.txt AC 1091 ms 193436 KB
s2_22.txt AC 1110 ms 190452 KB
s2_23.txt AC 1114 ms 195628 KB
s2_24.txt AC 1021 ms 223816 KB
s2_25.txt AC 2037 ms 396216 KB
s2_26.txt TLE 3190 ms 551864 KB
s2_27.txt TLE 3190 ms 559032 KB
s2_28.txt TLE 3189 ms 545976 KB
s2_29.txt TLE 3189 ms 555320 KB
s2_30.txt TLE 3189 ms 553528 KB
s2_31.txt TLE 3187 ms 512184 KB
s2_32.txt TLE 3186 ms 514236 KB