222942026-01-14 20:51:10helloworldHírlánccpp17Accepted 80/8079ms3520 KiB




#include <bits/stdc++.h>
using namespace std;

int N;
int C[300000];
int dp[300000];
int path[300000];
int pos[300000];

int main() {

    cin >> N;
    for (int i = 1; i <= N; i++) cin >> C[i];

    for (int i = 1; i <= N; i++) pos[i] = -1;

    for (int i = 1; i <= N; i++) {
        if (dp[i] != 0) continue;

        int len = 0;
        int u = i;

        while (dp[u] == 0 && pos[u] == -1) {
            pos[u] = len;
            path[len++] = u;
            u = C[u];
        }

        if (dp[u] != 0) {
            int v = dp[u];
            for (int j = len - 1; j >= 0; j--) {
                v++;
                dp[path[j]] = v;
            }
        } else {
            int start = pos[u];
            int cycle_len = len - start;

            for (int j = start; j < len; j++)
                dp[path[j]] = cycle_len;

            int v = cycle_len;
            for (int j = start - 1; j >= 0; j--) {
                v++;
                dp[path[j]] = v;
            }
        }

        for (int j = 0; j < len; j++)
            pos[path[j]] = -1;
    }

    int best = 1;
    for (int i = 2; i <= N; i++)
        if (dp[i] > dp[best]) best = i;

    cout << best << " " << dp[best] << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms316 KiB
3Accepted2ms316 KiB
4Accepted2ms316 KiB
5Accepted2ms316 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted2ms316 KiB
9Accepted2ms352 KiB
10Accepted2ms316 KiB
11Accepted2ms416 KiB
12Accepted2ms664 KiB
subtask318/18
13Accepted78ms2576 KiB
14Accepted79ms2560 KiB
15Accepted78ms2672 KiB
16Accepted79ms2828 KiB
17Accepted79ms2860 KiB
18Accepted79ms2880 KiB
19Accepted79ms2868 KiB
20Accepted79ms2976 KiB
21Accepted78ms3372 KiB
22Accepted79ms3520 KiB
subtask442/42
23Accepted1ms316 KiB
24Accepted2ms316 KiB
25Accepted2ms316 KiB
26Accepted2ms316 KiB
27Accepted2ms316 KiB
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted2ms316 KiB
31Accepted2ms352 KiB
32Accepted2ms316 KiB
33Accepted2ms416 KiB
34Accepted2ms664 KiB
35Accepted78ms2576 KiB
36Accepted79ms2560 KiB
37Accepted78ms2672 KiB
38Accepted79ms2828 KiB
39Accepted79ms2860 KiB
40Accepted79ms2880 KiB
41Accepted79ms2868 KiB
42Accepted79ms2976 KiB
43Accepted78ms3372 KiB
44Accepted79ms3520 KiB
45Accepted78ms2660 KiB
46Accepted79ms2612 KiB
47Accepted78ms2612 KiB
48Accepted78ms2724 KiB
49Accepted78ms2720 KiB
50Accepted79ms2836 KiB
51Accepted79ms2868 KiB
52Accepted78ms2684 KiB
53Accepted79ms2868 KiB
54Accepted79ms2868 KiB