217932026-01-14 07:53:20vyrallHírláncpypy3Time limit exceeded 20/80587ms89212 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())
            if stack:
                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
1Accepted37ms19692 KiB
subtask220/20
2Accepted65ms22340 KiB
3Accepted70ms22664 KiB
4Accepted65ms22140 KiB
5Accepted59ms22172 KiB
6Accepted75ms22080 KiB
7Accepted74ms22788 KiB
8Accepted61ms22448 KiB
9Accepted64ms22844 KiB
10Accepted61ms22756 KiB
11Accepted65ms22996 KiB
12Accepted61ms22996 KiB
subtask30/18
13Time limit exceeded579ms43720 KiB
14Accepted321ms89212 KiB
15Runtime error157ms44836 KiB
16Runtime error157ms44852 KiB
17Runtime error164ms44772 KiB
18Runtime error157ms44680 KiB
19Runtime error138ms44680 KiB
20Runtime error138ms44704 KiB
21Runtime error155ms44788 KiB
22Runtime error137ms44736 KiB
subtask40/42
23Accepted39ms19648 KiB
24Accepted65ms22340 KiB
25Accepted70ms22664 KiB
26Accepted65ms22140 KiB
27Accepted59ms22172 KiB
28Accepted75ms22080 KiB
29Accepted74ms22788 KiB
30Accepted61ms22448 KiB
31Accepted64ms22844 KiB
32Accepted61ms22756 KiB
33Accepted65ms22996 KiB
34Accepted61ms22996 KiB
35Time limit exceeded579ms43720 KiB
36Accepted321ms89212 KiB
37Runtime error157ms44836 KiB
38Runtime error157ms44852 KiB
39Runtime error164ms44772 KiB
40Runtime error157ms44680 KiB
41Runtime error138ms44680 KiB
42Runtime error138ms44704 KiB
43Runtime error155ms44788 KiB
44Runtime error137ms44736 KiB
45Time limit exceeded586ms43720 KiB
46Time limit exceeded586ms43768 KiB
47Time limit exceeded587ms43964 KiB
48Runtime error153ms44984 KiB
49Runtime error137ms44736 KiB
50Runtime error136ms44992 KiB
51Runtime error138ms44736 KiB
52Runtime error157ms44880 KiB
53Runtime error140ms44756 KiB
54Runtime error156ms44916 KiB