198342025-12-25 10:40:02szabel26Hírlánccpp17Accepted 80/80187ms17460 KiB
// hirlanc.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
using namespace std;

struct adat {
    vector<int>be;
    int sz = 0, tav = 0;
    bool lat = 0, kor = 0;
};

vector<adat>x;

pair<int, int>sol = { 0,0 };

int n, hossz, db, tav;

bool kor(int akt, int kezd)
{
    x[akt].lat = true;
    ++tav;
    if (x[x[akt].sz].kor)
    {
        x[akt].lat = false;
        x[akt].kor = false;
        return false;
    }
    else
    {
        if (x[akt].sz == kezd)
        {
            x[akt].lat = false;

            x[x[akt].sz].tav = tav + 1;
            x[akt].tav = tav;

            x[x[akt].sz].kor = true;
            x[akt].kor = true;
            return true;
        }
        else if (x[x[akt].sz].lat)
        {
            x[akt].lat = false;
            x[akt].kor = false;
            return false;
        }

        if (kor(x[akt].sz, kezd))
        {
            x[akt].lat = false;
            x[akt].tav = tav;
            x[akt].kor = true;
            return true;
        }
        else
        {
            x[akt].lat = false;
            x[akt].kor = false;
            return false;
        }
    }

}

void bejar(int akt, int tav)
{
    x[akt].lat = 1;
    if (x[akt].tav == 0 || tav > x[akt].tav) x[akt].tav = tav;
    for (auto& e : x[akt].be)
    {
        if (!x[e].lat && !x[e].kor)
        {
            bejar(e, tav + 1);
        }
    }
    x[akt].lat = 0;
}

int main()
{
    cin >> n;

    x.resize(n + 1);
    int a = 0;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a;
        x[i].sz = a;
        x[a].be.push_back(i);
    }

    for (int i = 1; i <= n; ++i)
    {
        tav = 0;
        if (!x[i].kor) kor(i, i);
    }

    for (int i = 1; i <= n; ++i)
    {
        if (x[i].kor) bejar(i, x[i].tav);
    }

    for (int i = 1; i <= n; ++i)
    {
        if (x[i].tav > sol.second)
        {
            sol.first = i;
            sol.second = x[i].tav;
        }
    }

    cout << sol.first << " " << sol.second;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms316 KiB
3Accepted2ms316 KiB
4Accepted2ms500 KiB
5Accepted2ms572 KiB
6Accepted2ms316 KiB
7Accepted2ms316 KiB
8Accepted2ms316 KiB
9Accepted2ms316 KiB
10Accepted2ms316 KiB
11Accepted2ms316 KiB
12Accepted2ms316 KiB
subtask318/18
13Accepted162ms14412 KiB
14Accepted140ms14360 KiB
15Accepted141ms14540 KiB
16Accepted166ms14664 KiB
17Accepted168ms15412 KiB
18Accepted136ms15412 KiB
19Accepted172ms15540 KiB
20Accepted141ms15404 KiB
21Accepted150ms16948 KiB
22Accepted127ms17460 KiB
subtask442/42
23Accepted1ms500 KiB
24Accepted2ms316 KiB
25Accepted2ms316 KiB
26Accepted2ms500 KiB
27Accepted2ms572 KiB
28Accepted2ms316 KiB
29Accepted2ms316 KiB
30Accepted2ms316 KiB
31Accepted2ms316 KiB
32Accepted2ms316 KiB
33Accepted2ms316 KiB
34Accepted2ms316 KiB
35Accepted162ms14412 KiB
36Accepted140ms14360 KiB
37Accepted141ms14540 KiB
38Accepted166ms14664 KiB
39Accepted168ms15412 KiB
40Accepted136ms15412 KiB
41Accepted172ms15540 KiB
42Accepted141ms15404 KiB
43Accepted150ms16948 KiB
44Accepted127ms17460 KiB
45Accepted144ms12340 KiB
46Accepted170ms12084 KiB
47Accepted148ms12084 KiB
48Accepted172ms12280 KiB
49Accepted180ms12336 KiB
50Accepted187ms12596 KiB
51Accepted184ms13100 KiB
52Accepted170ms13208 KiB
53Accepted152ms12852 KiB
54Accepted146ms13108 KiB