217922026-01-14 07:50:51vyrallHírláncpypy3Time limit exceeded 20/80587ms89240 KiB
n = int(input())
hirlanc = [0] + [int(i) for i in input().split()]
count = [-1] + n * [0]
state = [''] + n * ["U"]
stack = []
circle = []

def update(cur):
    global circle
    if "A" in state[cur]:
        if "E" not in state[hirlanc[cur]]:
            stack.append(cur)
            while stack and stack[-1] not in circle:
                circle.append(stack.pop())
            stack.pop()
            for i in circle:
                count[i] = len(circle)
                state[i] = "E"
            circle = []
            if stack:
                update(stack[-1])
        else:
            count[cur] = count[hirlanc[cur]] + 1
            state[cur] = "E"
            if stack:
                update(stack.pop())
    elif "E" in state[cur]:
        if stack:
            last = stack.pop()
            count[last] = count[cur] + 1
            state[last] = "E"
            update(last)
    else:
        state[cur] = "A"
        stack.append(cur)
        update(hirlanc[cur])

while 0 in count:
    idx = count.index(0)
    update(idx)
    # print(f'stack: {stack}, circle: {circle}, state: {state}, count: {count}')

mx = max(count)
print(count.index(mx), mx)
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted45ms19692 KiB
subtask220/20
2Accepted68ms22000 KiB
3Accepted76ms22716 KiB
4Accepted68ms22096 KiB
5Accepted61ms22196 KiB
6Accepted68ms22248 KiB
7Accepted71ms22920 KiB
8Accepted67ms22440 KiB
9Accepted65ms22740 KiB
10Accepted68ms22760 KiB
11Accepted63ms22876 KiB
12Accepted70ms23012 KiB
subtask30/18
13Time limit exceeded586ms43768 KiB
14Accepted326ms89240 KiB
15Runtime error157ms44904 KiB
16Runtime error157ms44884 KiB
17Runtime error165ms44728 KiB
18Runtime error157ms44840 KiB
19Runtime error158ms44880 KiB
20Runtime error138ms44708 KiB
21Runtime error158ms44744 KiB
22Runtime error137ms44736 KiB
subtask40/42
23Accepted39ms19668 KiB
24Accepted68ms22000 KiB
25Accepted76ms22716 KiB
26Accepted68ms22096 KiB
27Accepted61ms22196 KiB
28Accepted68ms22248 KiB
29Accepted71ms22920 KiB
30Accepted67ms22440 KiB
31Accepted65ms22740 KiB
32Accepted68ms22760 KiB
33Accepted63ms22876 KiB
34Accepted70ms23012 KiB
35Time limit exceeded586ms43768 KiB
36Accepted326ms89240 KiB
37Runtime error157ms44904 KiB
38Runtime error157ms44884 KiB
39Runtime error165ms44728 KiB
40Runtime error157ms44840 KiB
41Runtime error158ms44880 KiB
42Runtime error138ms44708 KiB
43Runtime error158ms44744 KiB
44Runtime error137ms44736 KiB
45Time limit exceeded587ms43680 KiB
46Time limit exceeded587ms43824 KiB
47Time limit exceeded584ms43968 KiB
48Runtime error153ms44988 KiB
49Runtime error153ms44752 KiB
50Runtime error136ms44728 KiB
51Runtime error140ms44844 KiB
52Runtime error137ms44804 KiB
53Runtime error156ms44752 KiB
54Runtime error156ms44924 KiB