// 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 = 0, tav = 0;
bool lat = 0, kor = 0;
};
vector<adat>x;
pair<int, int>sol = { 0,0 };
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;
return 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);
}
}
}
int main()
{
cin >> n;
x.resize(n + 1);
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;
}
}
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 | Accepted | 1ms | 316 KiB | ||||
| subtask2 | 0/20 | ||||||
| 2 | Wrong answer | 2ms | 316 KiB | ||||
| 3 | Accepted | 2ms | 316 KiB | ||||
| 4 | Wrong answer | 2ms | 316 KiB | ||||
| 5 | Accepted | 2ms | 316 KiB | ||||
| 6 | Wrong answer | 2ms | 316 KiB | ||||
| 7 | Accepted | 2ms | 316 KiB | ||||
| 8 | Wrong answer | 2ms | 316 KiB | ||||
| 9 | Wrong answer | 2ms | 316 KiB | ||||
| 10 | Accepted | 2ms | 492 KiB | ||||
| 11 | Accepted | 2ms | 508 KiB | ||||
| 12 | Accepted | 2ms | 316 KiB | ||||
| subtask3 | 18/18 | ||||||
| 13 | Accepted | 145ms | 14392 KiB | ||||
| 14 | Accepted | 165ms | 14384 KiB | ||||
| 15 | Accepted | 166ms | 14572 KiB | ||||
| 16 | Accepted | 145ms | 14720 KiB | ||||
| 17 | Accepted | 170ms | 15412 KiB | ||||
| 18 | Accepted | 168ms | 15412 KiB | ||||
| 19 | Accepted | 148ms | 15532 KiB | ||||
| 20 | Accepted | 148ms | 15508 KiB | ||||
| 21 | Accepted | 146ms | 16864 KiB | ||||
| 22 | Accepted | 152ms | 17404 KiB | ||||
| subtask4 | 0/42 | ||||||
| 23 | Accepted | 1ms | 508 KiB | ||||
| 24 | Wrong answer | 2ms | 316 KiB | ||||
| 25 | Accepted | 2ms | 316 KiB | ||||
| 26 | Wrong answer | 2ms | 316 KiB | ||||
| 27 | Accepted | 2ms | 316 KiB | ||||
| 28 | Wrong answer | 2ms | 316 KiB | ||||
| 29 | Accepted | 2ms | 316 KiB | ||||
| 30 | Wrong answer | 2ms | 316 KiB | ||||
| 31 | Wrong answer | 2ms | 316 KiB | ||||
| 32 | Accepted | 2ms | 492 KiB | ||||
| 33 | Accepted | 2ms | 508 KiB | ||||
| 34 | Accepted | 2ms | 316 KiB | ||||
| 35 | Accepted | 145ms | 14392 KiB | ||||
| 36 | Accepted | 165ms | 14384 KiB | ||||
| 37 | Accepted | 166ms | 14572 KiB | ||||
| 38 | Accepted | 145ms | 14720 KiB | ||||
| 39 | Accepted | 170ms | 15412 KiB | ||||
| 40 | Accepted | 168ms | 15412 KiB | ||||
| 41 | Accepted | 148ms | 15532 KiB | ||||
| 42 | Accepted | 148ms | 15508 KiB | ||||
| 43 | Accepted | 146ms | 16864 KiB | ||||
| 44 | Accepted | 152ms | 17404 KiB | ||||
| 45 | Accepted | 185ms | 12340 KiB | ||||
| 46 | Accepted | 185ms | 12084 KiB | ||||
| 47 | Accepted | 182ms | 12084 KiB | ||||
| 48 | Wrong answer | 150ms | 12208 KiB | ||||
| 49 | Accepted | 170ms | 12340 KiB | ||||
| 50 | Wrong answer | 164ms | 12424 KiB | ||||
| 51 | Accepted | 167ms | 13060 KiB | ||||
| 52 | Wrong answer | 203ms | 13092 KiB | ||||
| 53 | Wrong answer | 162ms | 12852 KiB | ||||
| 54 | Wrong answer | 162ms | 13108 KiB | ||||