-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10265.cpp
60 lines (53 loc) · 1.03 KB
/
10265.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
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
int N, K; int arr[1001];
int counts[1001];
int parent[1001];
int mark[1001];
int cycle[1001];
int no_cycle[1001];
int m;
void dfs(int x) {
if (!parent[x] && parent[arr[x]]) {
parent[x] = parent[arr[x]];
no_cycle[parent[x]]++;
return;
}
if (counts[x] == 2) return;
counts[x]++;
parent[x] = m;
parent[arr[x]] = m;
if (counts[x] == 1) no_cycle[m]++;
if (counts[x] == 2) cycle[m]++;
dfs(arr[x]);
}
void solve(){
cin >> N >> K;for (int i = 1; i <= N; i++) cin >> arr[i];
for (int i = 1; i <= N; i++) {
m = i;
if (!parent[i]) dfs(i);
}
bool visited[1001] = {0};
visited[0] = true;
for (int i = 1; i <= N; i++) {
for (int j = K; j >= 0; j--) {
for (int w = cycle[i]; w <= no_cycle[i]; w++) {
if (j >= w) visited[j] |= visited[j - w];
}
}
}
for (int i = K; i >= 0; i--) {
if (visited[i]) {
cout << i;
break;
}
}
}
int main()
{
solve();
return 0;
}