230922026-01-16 11:53:54badamHírlánccpp17Accepted 80/8037ms3744 KiB
#include <iostream>
#include <vector>

using namespace std;

// Globális változók a memória miatt (gyorsabb)
vector<int> t;
vector<int> eredmeny;
vector<int> path;
vector<int> path_idx;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    t.resize(n);
    for(int i = 0; i < n; i++)
    {
        cin >> t[i];
        t[i]--;
    }

    eredmeny.assign(n, 0);
    path_idx.assign(n, -1);
    path.reserve(n);

    int leghosszabb_ut = 0;
    int legjobb_kezdes = 0;

    for(int i = 0; i < n; i++)
    {
        if (eredmeny[i] != 0) continue;

        int jelenlegi = i;
        path.clear();

        while (true)
        {
            if (eredmeny[jelenlegi] != 0)
            {
                int hossz = eredmeny[jelenlegi];
                for (int j = path.size() - 1; j >= 0; j--)
                {
                    hossz++;
                    eredmeny[path[j]] = hossz;
                    path_idx[path[j]] = -1;
                }
                break;
            }
            if (path_idx[jelenlegi] != -1)
            {
                int kor_start = path_idx[jelenlegi];
                int kor_merete = path.size() - kor_start;
                for (int j = kor_start; j < path.size(); j++)
                {
                    eredmeny[path[j]] = kor_merete;
                    path_idx[path[j]] = -1;
                }
                int aktualis_hossz = kor_merete;
                for (int j = kor_start - 1; j >= 0; j--)
                {
                    aktualis_hossz++;
                    eredmeny[path[j]] = aktualis_hossz;
                    path_idx[path[j]] = -1;
                }
                break;
            }
            path_idx[jelenlegi] = path.size();
            path.push_back(jelenlegi);
            jelenlegi = t[jelenlegi];
        }
    }
    for(int i = 0; i < n; i++)
    {
        if (eredmeny[i] > leghosszabb_ut)
        {
            leghosszabb_ut = eredmeny[i];
            legjobb_kezdes = i + 1;
        }
    }
    cout << legjobb_kezdes << " " << leghosszabb_ut;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted1ms332 KiB
3Accepted1ms316 KiB
4Accepted1ms316 KiB
5Accepted1ms316 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted1ms332 KiB
9Accepted1ms316 KiB
10Accepted1ms316 KiB
11Accepted1ms316 KiB
12Accepted1ms552 KiB
subtask318/18
13Accepted35ms2760 KiB
14Accepted35ms2756 KiB
15Accepted35ms2816 KiB
16Accepted35ms2800 KiB
17Accepted35ms2880 KiB
18Accepted35ms3036 KiB
19Accepted37ms2876 KiB
20Accepted37ms3012 KiB
21Accepted35ms3380 KiB
22Accepted37ms3744 KiB
subtask442/42
23Accepted1ms316 KiB
24Accepted1ms332 KiB
25Accepted1ms316 KiB
26Accepted1ms316 KiB
27Accepted1ms316 KiB
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted1ms332 KiB
31Accepted1ms316 KiB
32Accepted1ms316 KiB
33Accepted1ms316 KiB
34Accepted1ms552 KiB
35Accepted35ms2760 KiB
36Accepted35ms2756 KiB
37Accepted35ms2816 KiB
38Accepted35ms2800 KiB
39Accepted35ms2880 KiB
40Accepted35ms3036 KiB
41Accepted37ms2876 KiB
42Accepted37ms3012 KiB
43Accepted35ms3380 KiB
44Accepted37ms3744 KiB
45Accepted34ms2760 KiB
46Accepted34ms2608 KiB
47Accepted34ms2608 KiB
48Accepted34ms2756 KiB
49Accepted34ms2756 KiB
50Accepted35ms2884 KiB
51Accepted35ms2760 KiB
52Accepted35ms2756 KiB
53Accepted35ms2956 KiB
54Accepted34ms2868 KiB