197822025-12-22 16:23:10szabel26Hírlánccpp17Runtime error 0/80226ms17764 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 error4ms500 KiB
subtask20/20
2Runtime error4ms820 KiB
3Runtime error4ms580 KiB
4Runtime error4ms572 KiB
5Runtime error4ms564 KiB
6Runtime error4ms564 KiB
7Runtime error4ms780 KiB
8Runtime error4ms564 KiB
9Runtime error4ms564 KiB
10Runtime error4ms760 KiB
11Runtime error4ms564 KiB
12Runtime error4ms756 KiB
subtask30/18
13Runtime error115ms14388 KiB
14Runtime error107ms14400 KiB
15Runtime error108ms14644 KiB
16Runtime error119ms14636 KiB
17Runtime error119ms15048 KiB
18Runtime error135ms14900 KiB
19Runtime error119ms14832 KiB
20Runtime error136ms14900 KiB
21Runtime error209ms17204 KiB
22Runtime error226ms17764 KiB
subtask40/42
23Runtime error4ms508 KiB
24Runtime error4ms820 KiB
25Runtime error4ms580 KiB
26Runtime error4ms572 KiB
27Runtime error4ms564 KiB
28Runtime error4ms564 KiB
29Runtime error4ms780 KiB
30Runtime error4ms564 KiB
31Runtime error4ms564 KiB
32Runtime error4ms760 KiB
33Runtime error4ms564 KiB
34Runtime error4ms756 KiB
35Runtime error115ms14388 KiB
36Runtime error107ms14400 KiB
37Runtime error108ms14644 KiB
38Runtime error119ms14636 KiB
39Runtime error119ms15048 KiB
40Runtime error135ms14900 KiB
41Runtime error119ms14832 KiB
42Runtime error136ms14900 KiB
43Runtime error209ms17204 KiB
44Runtime error226ms17764 KiB
45Runtime error111ms12340 KiB
46Runtime error125ms12360 KiB
47Runtime error127ms12188 KiB
48Runtime error114ms12340 KiB
49Runtime error119ms12344 KiB
50Runtime error128ms12688 KiB
51Runtime error142ms13100 KiB
52Runtime error136ms13368 KiB
53Runtime error157ms12852 KiB
54Runtime error171ms13364 KiB