229502026-01-16 09:28:44ZsoltHírlánccpp17Accepted 80/80116ms16080 KiB
#include <bits/stdc++.h>

using namespace std;

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

    int n,x;
    cin>>n;
    vector<vector<int>>g(n+1);
    vector<int>utak(n+1);
    vector<int>hova(n+1,0);
    for(int i=1; i<=n; i++)
    {
        cin>>x;
        g[x].push_back(i);
        utak[i]=x;
        hova[x]++;
    }
    vector<int>korben(n+1,1);
    queue<int>q;
    for(int i=1; i<=n; i++)
    {
        if(hova[i]==0)
        {
            q.push(i);
        }
    }
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        korben[x]=0;
        hova[utak[x]]--;
        if(hova[utak[x]]==0)
        {
            q.push(utak[x]);
        }
    }
    vector<int>volt(n+1,0);
    vector<int>dp(n+1,0);
    int akt,db;
    for(int i=1; i<=n; i++)
    {
        if(korben[i] && volt[i]==0)
        {
            akt=i;
            db=0;
            do
            {
                volt[akt]++;
                akt=utak[akt];
                db++;
            }
            while(akt!=i);
            akt=i;
            do
            {
                dp[akt]=db;
                akt=utak[akt];
            }
            while(akt!=i);
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(korben[i]==1)
        {
            q.push(i);
        }
    }
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(int v:g[x])
        {
            if(korben[v]==0)
            {
                dp[v]=dp[x]+1;
                q.push(v);
            }
        }
    }
    int k=1,maxdb=dp[1];
    for(int i=2; i<=n; i++)
    {
        if(dp[i]>maxdb)
            {
                maxdb=dp[i];
                k=i;
            }
    }
    cout<<k<<" "<<maxdb;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms316 KiB
3Accepted2ms316 KiB
4Accepted2ms316 KiB
5Accepted2ms756 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted1ms316 KiB
9Accepted1ms316 KiB
10Accepted1ms564 KiB
11Accepted1ms564 KiB
12Accepted1ms564 KiB
subtask318/18
13Accepted96ms15920 KiB
14Accepted79ms15956 KiB
15Accepted97ms16004 KiB
16Accepted81ms16080 KiB
17Accepted101ms15968 KiB
18Accepted81ms15924 KiB
19Accepted81ms15924 KiB
20Accepted97ms16036 KiB
21Accepted71ms15928 KiB
22Accepted82ms15960 KiB
subtask442/42
23Accepted1ms316 KiB
24Accepted2ms316 KiB
25Accepted2ms316 KiB
26Accepted2ms316 KiB
27Accepted2ms756 KiB
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted1ms316 KiB
31Accepted1ms316 KiB
32Accepted1ms564 KiB
33Accepted1ms564 KiB
34Accepted1ms564 KiB
35Accepted96ms15920 KiB
36Accepted79ms15956 KiB
37Accepted97ms16004 KiB
38Accepted81ms16080 KiB
39Accepted101ms15968 KiB
40Accepted81ms15924 KiB
41Accepted81ms15924 KiB
42Accepted97ms16036 KiB
43Accepted71ms15928 KiB
44Accepted82ms15960 KiB
45Accepted114ms13348 KiB
46Accepted116ms13108 KiB
47Accepted116ms13108 KiB
48Accepted75ms13112 KiB
49Accepted86ms13108 KiB
50Accepted105ms13108 KiB
51Accepted112ms13620 KiB
52Accepted92ms13888 KiB
53Accepted90ms13112 KiB
54Accepted86ms13108 KiB