230932026-01-16 11:55:36badamHírlánccpp17Accepted 80/8037ms3460 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
2Accepted2ms316 KiB
3Accepted1ms316 KiB
4Accepted2ms316 KiB
5Accepted2ms316 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted1ms316 KiB
9Accepted2ms316 KiB
10Accepted1ms316 KiB
11Accepted1ms512 KiB
12Accepted2ms508 KiB
subtask318/18
13Accepted35ms2612 KiB
14Accepted34ms2760 KiB
15Accepted37ms2612 KiB
16Accepted35ms2804 KiB
17Accepted35ms2888 KiB
18Accepted37ms2864 KiB
19Accepted35ms2868 KiB
20Accepted35ms2868 KiB
21Accepted35ms3276 KiB
22Accepted37ms3460 KiB
subtask442/42
23Accepted1ms500 KiB
24Accepted2ms316 KiB
25Accepted1ms316 KiB
26Accepted2ms316 KiB
27Accepted2ms316 KiB
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted1ms316 KiB
31Accepted2ms316 KiB
32Accepted1ms316 KiB
33Accepted1ms512 KiB
34Accepted2ms508 KiB
35Accepted35ms2612 KiB
36Accepted34ms2760 KiB
37Accepted37ms2612 KiB
38Accepted35ms2804 KiB
39Accepted35ms2888 KiB
40Accepted37ms2864 KiB
41Accepted35ms2868 KiB
42Accepted35ms2868 KiB
43Accepted35ms3276 KiB
44Accepted37ms3460 KiB
45Accepted35ms2612 KiB
46Accepted34ms2632 KiB
47Accepted34ms2616 KiB
48Accepted34ms2756 KiB
49Accepted35ms2612 KiB
50Accepted35ms2992 KiB
51Accepted35ms2868 KiB
52Accepted34ms2852 KiB
53Accepted35ms2868 KiB
54Accepted34ms3112 KiB