109582024-04-20 18:48:2442Vállalati ügyeletpypy3Accepted 100/100418ms260216 KiB
from sys import stdin, stdout
input=stdin.readline

def main():
    N = int(input())
    P = [0]+list(map(int, input().split()))
    A = [0]+list(map(int, input().split()))
    res=[1]*(N+1)
    for i in range(N,0,-1):
        cur=A.pop()
        if isinstance(cur,int):
            cur={cur}
        while res[i] in cur:
            cur.remove(res[i])
            res[i]+=1
        res[P[i]]=max(res[i],res[P[i]])
        if isinstance(A[P[i]],int):
            cur.add(A[P[i]])
            A[P[i]]=cur
        else:
            if len(A[P[i]]) >= len(cur)-100:
                A[P[i]].update(cur)
                #A[P[i]]=A[P[i]].union(cur)
                #A[P[i]] |= cur
            else:
                cur.update(A[P[i]])
                A[P[i]] = cur
    for i in range(1,N+1):
        stdout.write(str(res[i])+' ')
    stdout.write('\n')
        
main()
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted39ms76660 KiB
2Accepted259ms181644 KiB
subtask25/5
3Accepted41ms76844 KiB
4Accepted48ms77040 KiB
5Accepted43ms77836 KiB
6Accepted39ms78260 KiB
subtask38/8
7Accepted41ms76844 KiB
8Accepted48ms77040 KiB
9Accepted43ms77836 KiB
10Accepted39ms78260 KiB
11Accepted180ms177364 KiB
12Accepted216ms177188 KiB
13Accepted189ms176848 KiB
14Accepted250ms180052 KiB
subtask412/12
15Accepted41ms76844 KiB
16Accepted48ms77040 KiB
17Accepted43ms77836 KiB
18Accepted39ms78260 KiB
19Accepted173ms182688 KiB
20Accepted202ms178164 KiB
21Accepted178ms179272 KiB
22Accepted201ms184904 KiB
23Accepted195ms182152 KiB
24Accepted194ms179452 KiB
subtask517/17
25Accepted41ms76844 KiB
26Accepted48ms77040 KiB
27Accepted43ms77836 KiB
28Accepted39ms78260 KiB
29Accepted64ms91468 KiB
30Accepted59ms91232 KiB
31Accepted61ms90976 KiB
32Accepted64ms91224 KiB
33Accepted65ms90804 KiB
34Accepted64ms91548 KiB
35Accepted65ms91668 KiB
36Accepted63ms91644 KiB
37Accepted61ms91236 KiB
38Accepted68ms91440 KiB
39Accepted65ms91508 KiB
40Accepted64ms91484 KiB
41Accepted63ms91732 KiB
subtask628/28
42Accepted250ms179656 KiB
43Accepted237ms183240 KiB
44Accepted233ms183368 KiB
45Accepted252ms183260 KiB
46Accepted231ms182476 KiB
47Accepted215ms182764 KiB
48Accepted215ms181512 KiB
49Accepted201ms185284 KiB
50Accepted195ms182588 KiB
subtask730/30
51Accepted39ms79684 KiB
52Accepted231ms184392 KiB
53Accepted41ms76844 KiB
54Accepted48ms77040 KiB
55Accepted43ms77836 KiB
56Accepted39ms78260 KiB
57Accepted180ms177364 KiB
58Accepted216ms177188 KiB
59Accepted189ms176848 KiB
60Accepted250ms180052 KiB
61Accepted173ms182688 KiB
62Accepted202ms178164 KiB
63Accepted178ms179272 KiB
64Accepted201ms184904 KiB
65Accepted195ms182152 KiB
66Accepted194ms179452 KiB
67Accepted64ms91468 KiB
68Accepted59ms91232 KiB
69Accepted61ms90976 KiB
70Accepted64ms91224 KiB
71Accepted65ms90804 KiB
72Accepted64ms91548 KiB
73Accepted65ms91668 KiB
74Accepted63ms91644 KiB
75Accepted61ms91236 KiB
76Accepted68ms91440 KiB
77Accepted65ms91508 KiB
78Accepted64ms91484 KiB
79Accepted63ms91732 KiB
80Accepted250ms179656 KiB
81Accepted237ms183240 KiB
82Accepted233ms183368 KiB
83Accepted252ms183260 KiB
84Accepted231ms182476 KiB
85Accepted215ms182764 KiB
86Accepted215ms181512 KiB
87Accepted201ms185284 KiB
88Accepted195ms182588 KiB
89Accepted347ms198836 KiB
90Accepted345ms196756 KiB
91Accepted395ms231432 KiB
92Accepted418ms260216 KiB
93Accepted245ms180868 KiB
94Accepted261ms182100 KiB
95Accepted231ms186612 KiB
96Accepted239ms179052 KiB
97Accepted226ms185064 KiB
98Accepted273ms180412 KiB
99Accepted268ms179384 KiB
100Accepted234ms178712 KiB
101Accepted254ms185032 KiB