197832025-12-22 16:29:00szabel26Hírlánccpp17Runtime error 0/80188ms17716 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, tav;
    bool lat, kor;
};

vector<adat>x;

pair<int, int>sol;

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[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].tav = tav;
            x[akt].kor = true;
        }
        else
        {
            x[akt].lat = false;
            x[akt].kor = false;
            return false;
        }
    }

}

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

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);
        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
1Runtime error3ms316 KiB
subtask20/20
2Runtime error4ms564 KiB
3Runtime error4ms588 KiB
4Runtime error4ms568 KiB
5Runtime error4ms564 KiB
6Runtime error4ms564 KiB
7Runtime error3ms564 KiB
8Runtime error4ms564 KiB
9Runtime error4ms564 KiB
10Runtime error4ms564 KiB
11Runtime error4ms564 KiB
12Runtime error4ms564 KiB
subtask30/18
13Runtime error111ms14536 KiB
14Runtime error111ms14388 KiB
15Runtime error103ms14448 KiB
16Runtime error103ms14644 KiB
17Runtime error112ms14992 KiB
18Runtime error127ms14900 KiB
19Runtime error112ms14900 KiB
20Runtime error123ms14900 KiB
21Runtime error165ms16948 KiB
22Runtime error188ms17716 KiB
subtask40/42
23Runtime error4ms508 KiB
24Runtime error4ms564 KiB
25Runtime error4ms588 KiB
26Runtime error4ms568 KiB
27Runtime error4ms564 KiB
28Runtime error4ms564 KiB
29Runtime error3ms564 KiB
30Runtime error4ms564 KiB
31Runtime error4ms564 KiB
32Runtime error4ms564 KiB
33Runtime error4ms564 KiB
34Runtime error4ms564 KiB
35Runtime error111ms14536 KiB
36Runtime error111ms14388 KiB
37Runtime error103ms14448 KiB
38Runtime error103ms14644 KiB
39Runtime error112ms14992 KiB
40Runtime error127ms14900 KiB
41Runtime error112ms14900 KiB
42Runtime error123ms14900 KiB
43Runtime error165ms16948 KiB
44Runtime error188ms17716 KiB
45Runtime error114ms12340 KiB
46Runtime error114ms12340 KiB
47Runtime error104ms12456 KiB
48Runtime error108ms12340 KiB
49Runtime error123ms12340 KiB
50Runtime error116ms12596 KiB
51Runtime error128ms13108 KiB
52Runtime error112ms13364 KiB
53Runtime error136ms12852 KiB
54Runtime error158ms13364 KiB