194612025-12-09 19:26:47zoliHírlánccpp11Accepted 80/8087ms11708 KiB
#include <iostream>

using namespace std;
int N;
int C[200001], t[200001]; //t[i]-tavolsag i-bol indulva
char all[200001]; //allapotok tombje, all[i]=0 nem volt meglatogatva, 'A' aktiv latogatas, 'V' mar lejart
bool kor;
void be()
{
    cin>>N;
    for(int i=1;i<=N;i++) cin>>C[i];
}
void bejar(int i, int k)
{
    all[i]='A'; //egy aktiv bejarasban vagyunk
    t[i]=k;   //ideiglenesen lementjuk a jelenlegi hosszt, kesobb felulirjuk
    if(all[C[i]]=='V') // a kovetkezoje mar VOLT
    {
        t[i]=t[C[i]]+1;
        all[i]='V';
    }
    else if (all[C[i]]=='A') //egy korben vagyunk
        {
            t[i]=k-t[C[i]]+1;
            all[C[i]]='K';
            kor=true; //korkezdo
            all[i]='V';
        }
        else //nem volt meg latogatva
        {
            bejar(C[i],k+1);

            if(all[i]=='A'&& all[C[i]]=='V' && kor)
            {
                t[i]=t[C[i]];
                all[i]='V';
            }
            else if (all[i]=='K'&& all[C[i]]=='V' && kor)
            {
                kor=false;
                t[i]=t[C[i]];
                all[i]='V';
            }
            else if (all[i]=='A'&& all[C[i]]=='V' && !kor)
            {
                t[i]=t[C[i]]+1;
                all[i]='V';
            }else
            {
               // cout<<"mas allapot";
            }
        }
}
void keresmax()
{
    int maxi=1;
    for(int i=2;i<=N;i++)
        if(t[i]>t[maxi]) maxi=i;
    cout<<maxi<<" "<<t[maxi];
}
void feldolgoz()
{
    int i=1;
    while(i<=N)
    {
        if(all[i]=='V') i++;   // keresunk meg nem latogatott csomopontot
        else
        {
            bejar(i,0);   // onnan inditunk egy bejarast
        }
    }
}

int main()
{
    be();
    feldolgoz();
    keresmax();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms316 KiB
3Accepted2ms616 KiB
4Accepted2ms500 KiB
5Accepted2ms316 KiB
6Accepted2ms424 KiB
7Accepted1ms316 KiB
8Accepted2ms316 KiB
9Accepted1ms416 KiB
10Accepted1ms404 KiB
11Accepted2ms416 KiB
12Accepted2ms316 KiB
subtask318/18
13Accepted75ms2100 KiB
14Accepted75ms2100 KiB
15Accepted78ms2356 KiB
16Accepted76ms2880 KiB
17Accepted81ms5536 KiB
18Accepted82ms5376 KiB
19Accepted79ms5172 KiB
20Accepted79ms5296 KiB
21Accepted82ms9776 KiB
22Accepted87ms11708 KiB
subtask442/42
23Accepted1ms508 KiB
24Accepted2ms316 KiB
25Accepted2ms616 KiB
26Accepted2ms500 KiB
27Accepted2ms316 KiB
28Accepted2ms424 KiB
29Accepted1ms316 KiB
30Accepted2ms316 KiB
31Accepted1ms416 KiB
32Accepted1ms404 KiB
33Accepted2ms416 KiB
34Accepted2ms316 KiB
35Accepted75ms2100 KiB
36Accepted75ms2100 KiB
37Accepted78ms2356 KiB
38Accepted76ms2880 KiB
39Accepted81ms5536 KiB
40Accepted82ms5376 KiB
41Accepted79ms5172 KiB
42Accepted79ms5296 KiB
43Accepted82ms9776 KiB
44Accepted87ms11708 KiB
45Accepted75ms2100 KiB
46Accepted75ms2100 KiB
47Accepted74ms2100 KiB
48Accepted75ms3684 KiB
49Accepted75ms2592 KiB
50Accepted76ms3380 KiB
51Accepted76ms4844 KiB
52Accepted76ms3380 KiB
53Accepted78ms4460 KiB
54Accepted79ms5180 KiB