-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboj.1135.cc
64 lines (54 loc) · 1.01 KB
/
boj.1135.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef const ll cll;
typedef queue<ll> qll;
typedef queue<pll> qpll;
typedef priority_queue<ll> pqll;
typedef priority_queue<pll> pqpll;
typedef vector<ll> vll;
cll MAX_N = 50;
ll n, child[MAX_N][MAX_N] = {}, dp[MAX_N] = {};
ll search(ll k)
{
if (dp[k] >= 0)
{
return dp[k];
}
dp[k] = 0;
pqll pq;
for (ll i = 0; i < n; ++i)
{
if (child[k][i])
{
pq.push(search(i));
}
}
for (ll i = 1; !pq.empty(); ++i)
{
dp[k] = max(dp[k], i + pq.top());
pq.pop();
}
return dp[k];
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (ll a, i = 0; i < n; ++i)
{
cin >> a;
if (a >= 0)
{
child[a][i] = 1;
}
dp[i] = -1;
}
cout << search(0);
return 0;
}