195272025-12-12 16:33:13szabelrHírlánccpp17Accepted 80/8079ms3540 KiB
// hirlanc.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <set>
using namespace std;

int main()
{
    int n,x,tanulo=1;
    cin >> n;
    vector<int> pairs(n+1);

    int best[200001];
    for (int i = 1; i <= n; i++)
    {
        cin >> x;
        pairs[i]=x;
        best[i] = 0;
    }
    
    int maxi = 0;
    vector<int> pontok(n + 1);
    vector<int> hely(n + 1, -1);
    for (int i = 1; i <= n; i++)
    {
        if (best[i] == 0) {
            int k = i;
            int og = i;
            int count = 0;
            
            while (hely[k]==-1 and best[k]==0)
            {
                pontok[count] = k;
                count++;
                hely[k] = count - 1;
                k = pairs[k];
            }
            if (best[k] != 0)
            {
                int a = best[k] + 1;
                for (int y = count - 1; y >= 0; y--)
                {
                    best[pontok[y]] = a;
                    a++;
                }
            }
            else
            {
                int size = count - hely[k];
                for (int j = hely[k]; j <= count - 1; j++)
                {
                    best[pontok[j]] = size;

                }
                int a = best[k] + 1;
                for (int j = hely[k] - 1; j >= 0; j--)
                {
                    best[pontok[j]] = a;
                    a++;

                }
            }
            
            if (best[i] > maxi)
            {
                maxi = best[i];
                tanulo = i;
            }
        }
    }
    cout << tanulo<<" "<<maxi;
}

// 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
2Accepted1ms508 KiB
3Accepted1ms316 KiB
4Accepted1ms316 KiB
5Accepted1ms508 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted2ms316 KiB
9Accepted1ms372 KiB
10Accepted1ms316 KiB
11Accepted1ms316 KiB
12Accepted1ms316 KiB
subtask318/18
13Accepted78ms3404 KiB
14Accepted78ms3384 KiB
15Accepted78ms3380 KiB
16Accepted78ms3384 KiB
17Accepted79ms3328 KiB
18Accepted79ms3396 KiB
19Accepted78ms3380 KiB
20Accepted79ms3516 KiB
21Accepted78ms3352 KiB
22Accepted76ms3312 KiB
subtask442/42
23Accepted1ms500 KiB
24Accepted1ms508 KiB
25Accepted1ms316 KiB
26Accepted1ms316 KiB
27Accepted1ms508 KiB
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted2ms316 KiB
31Accepted1ms372 KiB
32Accepted1ms316 KiB
33Accepted1ms316 KiB
34Accepted1ms316 KiB
35Accepted78ms3404 KiB
36Accepted78ms3384 KiB
37Accepted78ms3380 KiB
38Accepted78ms3384 KiB
39Accepted79ms3328 KiB
40Accepted79ms3396 KiB
41Accepted78ms3380 KiB
42Accepted79ms3516 KiB
43Accepted78ms3352 KiB
44Accepted76ms3312 KiB
45Accepted78ms3404 KiB
46Accepted78ms3384 KiB
47Accepted76ms3384 KiB
48Accepted75ms3484 KiB
49Accepted78ms3380 KiB
50Accepted76ms3540 KiB
51Accepted76ms3380 KiB
52Accepted78ms3476 KiB
53Accepted78ms3532 KiB
54Accepted76ms3380 KiB