// 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
| Subtask | Sum | Test | Verdict | Time | Memory | ||
|---|---|---|---|---|---|---|---|
| subtask1 | 0/0 | ||||||
| 1 | Runtime error | 3ms | 316 KiB | ||||
| subtask2 | 0/20 | ||||||
| 2 | Runtime error | 4ms | 564 KiB | ||||
| 3 | Runtime error | 4ms | 756 KiB | ||||
| 4 | Runtime error | 4ms | 748 KiB | ||||
| 5 | Runtime error | 4ms | 564 KiB | ||||
| 6 | Runtime error | 4ms | 564 KiB | ||||
| 7 | Runtime error | 4ms | 820 KiB | ||||
| 8 | Runtime error | 4ms | 564 KiB | ||||
| 9 | Runtime error | 4ms | 564 KiB | ||||
| 10 | Runtime error | 4ms | 568 KiB | ||||
| 11 | Runtime error | 4ms | 564 KiB | ||||
| 12 | Runtime error | 4ms | 564 KiB | ||||
| subtask3 | 0/18 | ||||||
| 13 | Runtime error | 103ms | 14380 KiB | ||||
| 14 | Runtime error | 112ms | 14388 KiB | ||||
| 15 | Runtime error | 115ms | 14644 KiB | ||||
| 16 | Runtime error | 104ms | 14840 KiB | ||||
| 17 | Runtime error | 119ms | 14936 KiB | ||||
| 18 | Runtime error | 115ms | 14996 KiB | ||||
| 19 | Runtime error | 128ms | 14804 KiB | ||||
| 20 | Runtime error | 123ms | 14900 KiB | ||||
| 21 | Runtime error | 194ms | 16948 KiB | ||||
| 22 | Runtime error | 192ms | 17632 KiB | ||||
| subtask4 | 0/42 | ||||||
| 23 | Runtime error | 4ms | 316 KiB | ||||
| 24 | Runtime error | 4ms | 564 KiB | ||||
| 25 | Runtime error | 4ms | 756 KiB | ||||
| 26 | Runtime error | 4ms | 748 KiB | ||||
| 27 | Runtime error | 4ms | 564 KiB | ||||
| 28 | Runtime error | 4ms | 564 KiB | ||||
| 29 | Runtime error | 4ms | 820 KiB | ||||
| 30 | Runtime error | 4ms | 564 KiB | ||||
| 31 | Runtime error | 4ms | 564 KiB | ||||
| 32 | Runtime error | 4ms | 568 KiB | ||||
| 33 | Runtime error | 4ms | 564 KiB | ||||
| 34 | Runtime error | 4ms | 564 KiB | ||||
| 35 | Runtime error | 103ms | 14380 KiB | ||||
| 36 | Runtime error | 112ms | 14388 KiB | ||||
| 37 | Runtime error | 115ms | 14644 KiB | ||||
| 38 | Runtime error | 104ms | 14840 KiB | ||||
| 39 | Runtime error | 119ms | 14936 KiB | ||||
| 40 | Runtime error | 115ms | 14996 KiB | ||||
| 41 | Runtime error | 128ms | 14804 KiB | ||||
| 42 | Runtime error | 123ms | 14900 KiB | ||||
| 43 | Runtime error | 194ms | 16948 KiB | ||||
| 44 | Runtime error | 192ms | 17632 KiB | ||||
| 45 | Runtime error | 119ms | 12340 KiB | ||||
| 46 | Runtime error | 104ms | 12344 KiB | ||||
| 47 | Runtime error | 105ms | 12340 KiB | ||||
| 48 | Runtime error | 126ms | 12368 KiB | ||||
| 49 | Runtime error | 112ms | 12400 KiB | ||||
| 50 | Runtime error | 126ms | 12596 KiB | ||||
| 51 | Runtime error | 136ms | 13120 KiB | ||||
| 52 | Runtime error | 130ms | 13364 KiB | ||||
| 53 | Runtime error | 146ms | 12936 KiB | ||||
| 54 | Runtime error | 157ms | 13364 KiB | ||||