-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy patharborest.cpp
65 lines (57 loc) · 1.18 KB
/
arborest.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
# include <bits/stdc++.h>
# define NR 100005
using namespace std;
ifstream f("arborest.in");
ofstream g("arborest.out");
vector <int> v[NR];
struct elem {
int k, niv;
}st[NR];
bool cmp (elem x, elem y) {
return x.niv > y.niv;
}
int VV, ci, cs, mij, sol, n, K, i, nr;
int ap[NR], aux[NR], T[NR];
void DFSinit (int k, int niv) {
++VV; st[VV].niv=niv; st[VV].k=k;
for (auto &x: v[k])
DFSinit (x, niv+1);
}
void umple (int k) {
ap[k]=1;
for (auto &x: v[k])
if (! ap[x]) umple (x);
}
int numara (int K) {
int sol=0, tata;
memset (ap, 0, sizeof(ap));
for (int i=1; i<=VV; ++i) {
if (st[i].niv <= K) break;
if (! ap[st[i].k]) {
tata=st[i].k;
for (int j=1; j<K; ++j)
tata=T[tata];
umple (tata);
++sol;
}
}
return sol;
}
int main ()
{
f>>n>>K;
for (i=2; i<=n; ++i) {
f>>T[i];
v[T[i]].push_back(i);
}
DFSinit(1, 0); sort (st+1, st+VV+1, cmp);
ci=1; cs=n;
while (ci<=cs) {
mij=(ci+cs)/2;
nr=numara (mij);
if (nr<=K) cs=mij-1, sol=mij;
else ci=mij+1;
}
g<<sol<<"\n";
return 0;
}