-
Notifications
You must be signed in to change notification settings - Fork 152
/
316B2-EKG.cpp
43 lines (35 loc) · 1.05 KB
/
316B2-EKG.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
#include <cstdio>
#include <vector>
int main(){
const long N = 1010;
long n, x; scanf("%ld %ld", &n, &x);
std::vector<long> g(n + 1, -1);
std::vector<long> start;
for(long p = 1; p <= n; p++){
long a; scanf("%ld", &a);
if(a == 0){start.push_back(p);}
else{g[a] = p;}
}
std::vector<long> sizes;
long targetOrder(0);
for(long p = 0; p < start.size(); p++){
long node = start[p];
long target = (node == x) ? 1 : 0;
long count(1);
while(g[node] > 0){
node = g[node];
++count;
if(node == x){target = count;}
}
if(target == 0){sizes.push_back(count);}
else{targetOrder = target;}
}
std::vector<bool> possible(N, 0);
possible[targetOrder] = 1;
for(long p = 0; p < sizes.size(); p++){
long current = sizes[p];
for(long q = N; q >= current; q--){possible[q] = possible[q] | possible[q - current];}
}
for(long p = 0; p <= N; p++){if(possible[p]){printf("%ld\n", p);}}
return 0;
}