217982026-01-14 08:03:27vyrallHírláncpypy3Time limit exceeded 20/80592ms89236 KiB
from sys import setrecursionlimit

n = int(input())
hirlanc = [0] + [int(i) for i in input().split()]
count = [-1] + n * [0]
state = [''] + n * ["U"]
stack = []
circle = []
setrecursionlimit(10000)

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
1Accepted45ms19692 KiB
subtask220/20
2Accepted68ms22160 KiB
3Accepted75ms22680 KiB
4Accepted61ms22236 KiB
5Accepted59ms22228 KiB
6Accepted68ms22128 KiB
7Accepted64ms22948 KiB
8Accepted68ms22288 KiB
9Accepted74ms22768 KiB
10Accepted61ms22860 KiB
11Accepted68ms23004 KiB
12Accepted68ms23144 KiB
subtask30/18
13Time limit exceeded587ms43760 KiB
14Accepted367ms89236 KiB
15Time limit exceeded523ms55444 KiB
16Time limit exceeded589ms62352 KiB
17Runtime error212ms59400 KiB
18Runtime error193ms59288 KiB
19Runtime error194ms59280 KiB
20Runtime error194ms59288 KiB
21Runtime error216ms59288 KiB
22Runtime error214ms59288 KiB
subtask40/42
23Accepted43ms19652 KiB
24Accepted68ms22160 KiB
25Accepted75ms22680 KiB
26Accepted61ms22236 KiB
27Accepted59ms22228 KiB
28Accepted68ms22128 KiB
29Accepted64ms22948 KiB
30Accepted68ms22288 KiB
31Accepted74ms22768 KiB
32Accepted61ms22860 KiB
33Accepted68ms23004 KiB
34Accepted68ms23144 KiB
35Time limit exceeded587ms43760 KiB
36Accepted367ms89236 KiB
37Time limit exceeded523ms55444 KiB
38Time limit exceeded589ms62352 KiB
39Runtime error212ms59400 KiB
40Runtime error193ms59288 KiB
41Runtime error194ms59280 KiB
42Runtime error194ms59288 KiB
43Runtime error216ms59288 KiB
44Runtime error214ms59288 KiB
45Time limit exceeded583ms43720 KiB
46Time limit exceeded586ms43696 KiB
47Time limit exceeded584ms44056 KiB
48Time limit exceeded592ms47028 KiB
49Time limit exceeded592ms60464 KiB
50Runtime error405ms59944 KiB
51Runtime error194ms59284 KiB
52Runtime error386ms59800 KiB
53Runtime error192ms59304 KiB
54Runtime error215ms59284 KiB