forked from saketh-are/algo-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polandball_and_gifts.cpp
56 lines (39 loc) · 1.06 KB
/
polandball_and_gifts.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
// Problem: https://codeforces.com/problemset/problem/755/F
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
// {{{ misc/subset_sum.cpp }}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
vector<int> giver(N);
for (int i = 0; i < N; i++) {
cin >> giver[i];
--giver[i];
}
vector<int> cycle_lengths;
vector<bool> visited(N);
for (int i = 0; i < N; i++) {
if (visited[i])
continue;
int length = 0;
for (int t = i; !visited[t]; t = giver[t]) {
visited[t] = true;
length++;
}
cycle_lengths.push_back(length);
}
auto sums = subset_sum<1000000>(cycle_lengths.begin(), cycle_lengths.end());
int minimum = K + !sums[K];
int twos = 0, ones = 0;
for (int length : cycle_lengths) {
twos += length / 2;
ones += length & 1;
}
int maximum = 2 * min(K, twos) + min(ones, K - min(K, twos));
printf("%d %d\n", minimum, maximum);
return 0;
}