197762025-12-22 14:59:59szabel26Hírlánccpp17Time limit exceeded 38/80600ms6316 KiB
// hirlanc.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

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

struct adat {
    int be, sz;
    bool lat;
};

vector<adat>x, y;
vector<int>csp;

pair<int, int>sol;

int n, hossz;

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, { 0,0,0 });
    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 = i;
    }

    y = x;

    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 == false)
        {
            kor(i, i);
            if (hossz > sol.second)
            {
                sol.first = i;
                sol.second = 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
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms508 KiB
3Accepted2ms316 KiB
4Accepted2ms316 KiB
5Accepted3ms512 KiB
6Accepted3ms316 KiB
7Accepted4ms316 KiB
8Accepted4ms500 KiB
9Accepted8ms508 KiB
10Accepted2ms316 KiB
11Accepted1ms316 KiB
12Accepted1ms316 KiB
subtask318/18
13Accepted86ms4916 KiB
14Accepted82ms5104 KiB
15Accepted82ms5108 KiB
16Accepted86ms5104 KiB
17Accepted82ms4916 KiB
18Accepted92ms5096 KiB
19Accepted82ms4920 KiB
20Accepted83ms4916 KiB
21Accepted82ms4916 KiB
22Accepted79ms5132 KiB
subtask40/42
23Accepted1ms512 KiB
24Accepted2ms508 KiB
25Accepted2ms316 KiB
26Accepted2ms316 KiB
27Accepted3ms512 KiB
28Accepted3ms316 KiB
29Accepted4ms316 KiB
30Accepted4ms500 KiB
31Accepted8ms508 KiB
32Accepted2ms316 KiB
33Accepted1ms316 KiB
34Accepted1ms316 KiB
35Accepted86ms4916 KiB
36Accepted82ms5104 KiB
37Accepted82ms5108 KiB
38Accepted86ms5104 KiB
39Accepted82ms4916 KiB
40Accepted92ms5096 KiB
41Accepted82ms4920 KiB
42Accepted83ms4916 KiB
43Accepted82ms4916 KiB
44Accepted79ms5132 KiB
45Accepted114ms5552 KiB
46Accepted441ms5548 KiB
47Time limit exceeded583ms5740 KiB
48Time limit exceeded600ms5552 KiB
49Time limit exceeded583ms5552 KiB
50Time limit exceeded586ms5804 KiB
51Time limit exceeded577ms5808 KiB
52Time limit exceeded600ms5808 KiB
53Time limit exceeded580ms6060 KiB
54Time limit exceeded580ms6316 KiB