197802025-12-22 16:22:42szabel26Hírlánccpp17Runtime error 0/80194ms17632 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, y;
vector<int>csp;

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);
        }
    }
}

//void bejar(int akt)
//{
//    x[akt].lat = true;
//    y[akt].lat = true;
//    ++hossz;
//    if (x[x[akt].sz].lat == false)
//    {
//        bejar(x[akt].sz);
//    }
//    x[akt].lat = false;
//    return;
//}
//
//void kor(int akt, int kezd)
//{
//    y[akt].lat = true;
//    ++hossz;
//    if (y[y[akt].sz].lat == false)
//    {
//        kor(y[akt].sz, kezd);
//    }
//}

int main()
{
    cin >> n;

    x.resize(n + 1);
    //y.resize(n + 1, { 0,0,0 });
    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;
        }
    }

    /*for (int i = 1; i <= n; ++i)
    {
        cout << i << ": " << x[i].tav << endl;
    }*/

    //for (int i = 1; i <= n; ++i)
    //{
    //    if (x[i].be == 0)
    //    {
    //        csp.push_back(i);
    //    }
    //}

    //sol.first = 0;
    //sol.second = 0;
    //for (auto& e : csp)
    //{
    //    bejar(e);
    //    if (hossz > sol.second)
    //    {
    //        sol.first = e;
    //        sol.second = hossz;
    //    }
    //    hossz = 0;
    //}

    //for (int i = 1; i <= n; ++i)
    //{
    //    if (y[i].lat == 0) ++db;
    //}

    //for (int i = 1; i <= n; ++i)
    //{
    //    if (db <= sol.second)
    //    {
    //        break;
    //    }
    //    else
    //    {
    //        if (y[i].lat == false)
    //        {
    //            kor(i, i);
    //            if (hossz > sol.second)
    //            {
    //                sol.first = i;
    //                sol.second = hossz;
    //            }
    //            db += hossz;
    //            hossz = 0;
    //        }
    //    }
    //}
    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 error4ms756 KiB
4Runtime error4ms748 KiB
5Runtime error4ms564 KiB
6Runtime error4ms564 KiB
7Runtime error4ms820 KiB
8Runtime error4ms564 KiB
9Runtime error4ms564 KiB
10Runtime error4ms568 KiB
11Runtime error4ms564 KiB
12Runtime error4ms564 KiB
subtask30/18
13Runtime error103ms14380 KiB
14Runtime error112ms14388 KiB
15Runtime error115ms14644 KiB
16Runtime error104ms14840 KiB
17Runtime error119ms14936 KiB
18Runtime error115ms14996 KiB
19Runtime error128ms14804 KiB
20Runtime error123ms14900 KiB
21Runtime error194ms16948 KiB
22Runtime error192ms17632 KiB
subtask40/42
23Runtime error4ms316 KiB
24Runtime error4ms564 KiB
25Runtime error4ms756 KiB
26Runtime error4ms748 KiB
27Runtime error4ms564 KiB
28Runtime error4ms564 KiB
29Runtime error4ms820 KiB
30Runtime error4ms564 KiB
31Runtime error4ms564 KiB
32Runtime error4ms568 KiB
33Runtime error4ms564 KiB
34Runtime error4ms564 KiB
35Runtime error103ms14380 KiB
36Runtime error112ms14388 KiB
37Runtime error115ms14644 KiB
38Runtime error104ms14840 KiB
39Runtime error119ms14936 KiB
40Runtime error115ms14996 KiB
41Runtime error128ms14804 KiB
42Runtime error123ms14900 KiB
43Runtime error194ms16948 KiB
44Runtime error192ms17632 KiB
45Runtime error119ms12340 KiB
46Runtime error104ms12344 KiB
47Runtime error105ms12340 KiB
48Runtime error126ms12368 KiB
49Runtime error112ms12400 KiB
50Runtime error126ms12596 KiB
51Runtime error136ms13120 KiB
52Runtime error130ms13364 KiB
53Runtime error146ms12936 KiB
54Runtime error157ms13364 KiB