Submission #2708419


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]:
        S = Q[j]
        if not R:
            R = S
            continue

        # |R| > |S|
        if len(R) < len(S):
            R, S = S, R

        # dequeの小さい方から大きい方へマージする処理
        for k in range(len(S)):
            a0, a1, a2, c0 = S.popleft()
            b0, b1, b2, c1 = R[k]
            R[k] = [a0*b0 % MOD, (a0*b1 + a1*b0) % MOD, (a0*b2 + a1*b1 + a2*b0 + a1*b2 + a2*b2 + a2*b1) % MOD, c0+c1]
    # a0 <- a2
    for e in R:
        e[0] += e[2]
        e[2] = 0
    q.extend(R)
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 1016 Byte
Status TLE
Exec Time 3190 ms
Memory 568504 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 164 ms 38384 KB
00_example_02.txt AC 161 ms 38256 KB
00_example_03.txt AC 163 ms 38256 KB
s1_01.txt AC 168 ms 38384 KB
s1_02.txt AC 185 ms 40432 KB
s1_03.txt AC 203 ms 42480 KB
s1_04.txt AC 162 ms 38384 KB
s1_05.txt AC 171 ms 39280 KB
s1_06.txt AC 188 ms 42096 KB
s1_07.txt AC 165 ms 38256 KB
s1_08.txt AC 270 ms 61148 KB
s1_09.txt AC 175 ms 39792 KB
s1_10.txt AC 211 ms 43248 KB
s1_11.txt AC 168 ms 38256 KB
s1_12.txt AC 186 ms 41456 KB
s1_13.txt AC 160 ms 38256 KB
s1_14.txt AC 201 ms 43248 KB
s1_15.txt AC 207 ms 43120 KB
s1_16.txt AC 196 ms 42608 KB
s1_17.txt AC 202 ms 44272 KB
s2_01.txt AC 294 ms 60528 KB
s2_02.txt AC 225 ms 46188 KB
s2_03.txt AC 742 ms 142728 KB
s2_04.txt AC 487 ms 122620 KB
s2_05.txt AC 747 ms 142240 KB
s2_06.txt AC 928 ms 185964 KB
s2_07.txt AC 672 ms 157220 KB
s2_08.txt AC 389 ms 99644 KB
s2_09.txt AC 930 ms 183688 KB
s2_10.txt AC 856 ms 151008 KB
s2_11.txt AC 543 ms 184788 KB
s2_12.txt AC 190 ms 42992 KB
s2_13.txt TLE 3189 ms 555456 KB
s2_14.txt TLE 3188 ms 563552 KB
s2_15.txt TLE 3189 ms 560940 KB
s2_16.txt AC 233 ms 55024 KB
s2_17.txt AC 219 ms 46576 KB
s2_18.txt AC 471 ms 140936 KB
s2_19.txt AC 360 ms 118284 KB
s2_20.txt AC 1059 ms 197548 KB
s2_21.txt AC 996 ms 195100 KB
s2_22.txt AC 958 ms 195828 KB
s2_23.txt AC 1007 ms 194732 KB
s2_24.txt AC 931 ms 221896 KB
s2_25.txt AC 2018 ms 396472 KB
s2_26.txt TLE 3190 ms 568504 KB
s2_27.txt TLE 3189 ms 562232 KB
s2_28.txt TLE 3190 ms 562232 KB
s2_29.txt TLE 3187 ms 549184 KB
s2_30.txt TLE 3188 ms 558404 KB
s2_31.txt TLE 3186 ms 531392 KB
s2_32.txt TLE 3187 ms 531556 KB