# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
655402 | ShirAriel | Forensic (NOI12_forensic) | C++17 | 3 ms | 1492 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
vector<vector<ll>> branch;
vector<bool> bigchain;
ll getlen(ll v)
{
ll ans = 1;
for (ll i = 0; i < branch[v].size(); i++)
{
ll u = branch[v][i];
if (!bigchain[u])
ans = max(getlen(u) + 1, ans);
}
return ans;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n;
cin >> n;
vector<ll> chain(n);
for (ll i = 0; i < n; i++)
cin >> chain[i];
branch.resize(n);
for (ll i = 0; i < n; i++)
if (chain[i] != -1)
branch[chain[i]].push_back(i);
//find main chain
bool circle = false;
bigchain.resize(n, false);
ll v = chain[0], cnt=0;
bigchain[0] = true;
while (v!=-1 && !bigchain[v])
{
bigchain[v] = true;
v = chain[v];
cnt++;
}
if (v != -1)
circle = true;
//find branches
ll b = 0;
v = chain[0];
for (ll i = 0; i < cnt;i++)
{
b = max(b, getlen(v));
v = chain[v];
}
//find other chains
ll c = 0;
for (ll i = 0; i < n; i++)
if (chain[i] == -1 && !bigchain[i])
c = max(c, getlen(i) + 1);
if (circle&&c == 0)
cout << cnt + 1;
else
cout << cnt + max(c, (circle ? 0 : b));
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |