201312026-01-02 12:17:59VargaVivienHírlánccpp17Accepted 80/8087ms3872 KiB
#include <iostream>
#include <vector>

using namespace std;

vector <int> b, latogat, hossz, ut;

void bejaras(int start)
{
    ut.clear();
    int k;
    k = start;
    do
    {
        ut.push_back(k);
        latogat[k] = 1;
        k = b[k];
    }
    while(latogat[k] == 0);
    /*az elso kor egy tagja*/
    int db = 0, temp = k;
    if(latogat[k] == 1)
    {
        do
        {
            temp = b[temp];
            db++;
        }
        while (temp != k);
        do
        {
            latogat[temp] = 2;
            hossz[temp] = db;
            temp = b[temp];

        }
        while (temp != k);
    }
    for(int i = ut.size() - 1; i >= 0; i--)
    {
        if(latogat[ut[i]] == 1)
        {
            hossz[ut[i]] = hossz[b[ut[i]]] + 1;
            latogat[ut[i]] = 2;
        }
    }
}

int main()
{
    int n;
    cin >> n;
    b.resize(n + 1);
    for(int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    latogat.resize(n);
    fill(latogat.begin(), latogat.end() - 1, 0);
    hossz.resize(n);
    for(int i = 1; i <= n; i++)
    {
        if(latogat[i] == 0)
        {
            bejaras(i);
        }
    }
    int maxi = 0, index;
    for(int i = 1; i <= n; i++)
    {
        if(hossz[i] > maxi)
        {
            maxi = hossz[i];
            index = i;
        }
    }
    cout << index << " " << maxi;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms316 KiB
3Accepted2ms508 KiB
4Accepted2ms316 KiB
5Accepted2ms316 KiB
6Accepted2ms316 KiB
7Accepted2ms316 KiB
8Accepted1ms316 KiB
9Accepted2ms316 KiB
10Accepted2ms508 KiB
11Accepted2ms316 KiB
12Accepted2ms508 KiB
subtask318/18
13Accepted81ms2732 KiB
14Accepted82ms2728 KiB
15Accepted82ms2732 KiB
16Accepted82ms2728 KiB
17Accepted85ms3124 KiB
18Accepted83ms3248 KiB
19Accepted85ms3360 KiB
20Accepted82ms3368 KiB
21Accepted83ms3760 KiB
22Accepted85ms3872 KiB
subtask442/42
23Accepted1ms316 KiB
24Accepted2ms316 KiB
25Accepted2ms508 KiB
26Accepted2ms316 KiB
27Accepted2ms316 KiB
28Accepted2ms316 KiB
29Accepted2ms316 KiB
30Accepted1ms316 KiB
31Accepted2ms316 KiB
32Accepted2ms508 KiB
33Accepted2ms316 KiB
34Accepted2ms508 KiB
35Accepted81ms2732 KiB
36Accepted82ms2728 KiB
37Accepted82ms2732 KiB
38Accepted82ms2728 KiB
39Accepted85ms3124 KiB
40Accepted83ms3248 KiB
41Accepted85ms3360 KiB
42Accepted82ms3368 KiB
43Accepted83ms3760 KiB
44Accepted85ms3872 KiB
45Accepted79ms2528 KiB
46Accepted79ms2612 KiB
47Accepted78ms2628 KiB
48Accepted79ms2732 KiB
49Accepted79ms2868 KiB
50Accepted79ms2868 KiB
51Accepted82ms3176 KiB
52Accepted79ms2868 KiB
53Accepted79ms3128 KiB
54Accepted87ms3248 KiB