-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathpetsoft.cpp
85 lines (81 loc) · 2.23 KB
/
petsoft.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# include <fstream>
# include <algorithm>
# include <vector>
# include <cmath>
# define NR 1005
using namespace std;
ifstream f("petsoft.in");
ofstream g("petsoft.out");
vector <int> v[NR];
int i,j,n,m,VV,ci,cs,x;
int a[NR], A[NR][3], mat[NR][NR];
int maxim (int a, int b, int c, int d)
{
int nr1=max(a, b);
int nr2=max(c, d);
return max(nr1, nr2);
}
int maxx (int k, int VV)
{
int i,j;
for (j=1; j<=VV-1; ++j)
for (i=1; i<=VV-j; ++i)
{
ci=i; cs=i+j;
if (a[ci]==k)
{
mat[ci][cs]=max (mat[ci+1][cs], mat[ci][cs-1]+max(A[cs][0], A[cs][1]));//nu le iau
mat[ci][cs]=max (mat[ci][cs], mat[ci+1][cs-1]+(a[cs]-a[ci])+A[cs][0]); //le iau pe ambele
mat[ci][cs]=max (mat[ci][cs], mat[ci+1][cs-1]+max(A[cs][0], A[cs][1]));
}
else if (a[cs]==k)
{
mat[ci][cs]=max (mat[ci][cs-1], mat[ci+1][cs]+max(A[ci][0], A[ci][1]));//nu le iau
mat[ci][cs]=max (mat[ci][cs], mat[ci+1][cs-1]+(a[cs]-a[ci])+A[ci][0]); //le iau pe ambele
mat[ci][cs]=max (mat[ci][cs], mat[ci+1][cs-1]+max(A[ci][0], A[ci][1]));
}
else
{
mat[ci][cs]=max (mat[ci+1][cs]+max(A[ci][0], A[ci][1]), mat[ci][cs-1]+max(A[cs][0], A[cs][1]));//nu le iau
mat[ci][cs]=max (mat[ci][cs], mat[ci+1][cs-1]+(a[cs]-a[ci])+A[ci][0]+A[cs][0]); //le iau pe ambele
mat[ci][cs]=max (mat[ci][cs], mat[ci+1][cs-1]+max(A[ci][0], A[ci][1])+max(A[cs][0], A[cs][1]));
}
}
return mat[1][VV];
}
void DFS (int k)
{
int i;
for (i=0; i<v[k].size(); ++i)
DFS (v[k][i]);
if (v[k].size()==1)
{
A[k][0]=max(A[v[k][0]][0], A[v[k][0]][1]);
A[k][1]=abs(k-v[k][0]) + A[v[k][0]][0];
}
else
{
//fara el
VV=0;
for (i=0; i<v[k].size(); ++i)
a[++VV]=v[k][i];
sort (a+1, a+VV+1);
A[k][0]=maxx (k, VV);
//cu el
a[++VV]=k;
sort (a+1, a+VV+1);
A[k][1]=maxx (k, VV);
}
}
int main ()
{
f>>n;
for (i=2; i<=n; ++i)
{
f>>x;
v[x].push_back(i);
}
DFS (1);
g<<A[1][0];
return 0;
}