-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandyshop.cpp
54 lines (44 loc) · 960 Bytes
/
candyshop.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
void solve() {
int n, m, a, b;
cin >> n >> m >> a >> b;
set<int> unused;
vector<set<int>> child(n);
vector<int> mark(n);
int ans = 0;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
child[p].insert(i);
}
for (int i = 0; i < m; i++) {
mark[(a * i + b) % n]++;
}
function<void(int)> dfs = [&](int v) {
for (int to : child[v]) {
dfs(to);
}
unused.insert(v);
while (mark[v] > 0) {
auto top = unused.end();
ans += *(--top);
unused.erase(top);
}
};
dfs(0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
cout << "Case #" << i + 1 << ": ";
solve();
}
return 0;
}