197782025-12-22 15:10:08szabel26Hírlánccpp17Időlimit túllépés 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, db;

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 == 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
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva1ms316 KiB
subtask220/20
2Elfogadva2ms316 KiB
3Elfogadva2ms316 KiB
4Elfogadva2ms316 KiB
5Elfogadva3ms316 KiB
6Elfogadva3ms512 KiB
7Elfogadva3ms316 KiB
8Elfogadva4ms316 KiB
9Elfogadva8ms316 KiB
10Elfogadva2ms316 KiB
11Elfogadva2ms316 KiB
12Elfogadva2ms316 KiB
subtask318/18
13Elfogadva86ms5108 KiB
14Elfogadva82ms5108 KiB
15Elfogadva85ms5148 KiB
16Elfogadva82ms5128 KiB
17Elfogadva83ms5148 KiB
18Elfogadva86ms5112 KiB
19Elfogadva86ms4916 KiB
20Elfogadva82ms5108 KiB
21Elfogadva81ms5108 KiB
22Elfogadva81ms4916 KiB
subtask40/42
23Elfogadva2ms316 KiB
24Elfogadva2ms316 KiB
25Elfogadva2ms316 KiB
26Elfogadva2ms316 KiB
27Elfogadva3ms316 KiB
28Elfogadva3ms512 KiB
29Elfogadva3ms316 KiB
30Elfogadva4ms316 KiB
31Elfogadva8ms316 KiB
32Elfogadva2ms316 KiB
33Elfogadva2ms316 KiB
34Elfogadva2ms316 KiB
35Elfogadva86ms5108 KiB
36Elfogadva82ms5108 KiB
37Elfogadva85ms5148 KiB
38Elfogadva82ms5128 KiB
39Elfogadva83ms5148 KiB
40Elfogadva86ms5112 KiB
41Elfogadva86ms4916 KiB
42Elfogadva82ms5108 KiB
43Elfogadva81ms5108 KiB
44Elfogadva81ms4916 KiB
45Elfogadva115ms5748 KiB
46Elfogadva449ms5552 KiB
47Időlimit túllépés579ms5740 KiB
48Időlimit túllépés600ms5740 KiB
49Időlimit túllépés588ms5552 KiB
50Időlimit túllépés583ms5804 KiB
51Időlimit túllépés583ms5744 KiB
52Időlimit túllépés583ms5812 KiB
53Időlimit túllépés583ms6060 KiB
54Időlimit túllépés584ms6316 KiB