#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_K = 1e5 + 5;
vector <pair <int, int>> riped[MAX_K];
vector <int> graph[MAX_N];
int parent[MAX_N];
int depth[MAX_N];
bool dead[MAX_N];
pair <int, int> fruit[MAX_N];
long long sum;
void dfs(int u) {
for(auto v : graph[u]) {
depth[v] = depth[u] + 1;
dfs(v);
}
}
void dfs2(int u, int k) {
if(dead[u] == true) {
return;
}
if(fruit[u].first == k) {
sum += fruit[u].second;
}
dead[u] = true;
for(auto v : graph[u]) {
dfs2(v, k);
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M, K;
cin >> N >> M >> K;
for(int i = 2; i <= N; i++) {
cin >> parent[i];
graph[parent[i]].push_back(i);
}
dfs(1);
long long ans = 0;
bool sub2 = true;
for(int i = 1; i <= M; i++) {
int v, d, w;
cin >> v >> d >> w;
riped[d].emplace_back(v, w);
fruit[v] = make_pair(d, w);
if(graph[v].size() > 0) {
sub2 = false;
}
ans += w;
}
if(sub2 == true) {
cout << ans;
return 0;
}
ans = 0;
for(int i = 1; i <= K; i++) {
sort(riped[i].begin(), riped[i].end(), [&](const pair <int, int> &a, const pair <int, int> &b) {
return depth[a.first] > depth[b.first];
});
}
for(int mask = 0; mask < (1<<K); mask++) {
sum = 0;
for(int i = 1; i <= N; i++) {
dead[i] = false;
}
for(int k = 0; k < K; k++) {
if(mask & (1<<k)) {
for(auto [v, w] : riped[k + 1]) {
dfs2(v, k + 1);
}
}
}
ans = max(ans, sum);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
9780 KB |
Output is correct |
2 |
Correct |
29 ms |
10960 KB |
Output is correct |
3 |
Correct |
49 ms |
11680 KB |
Output is correct |
4 |
Correct |
52 ms |
10948 KB |
Output is correct |
5 |
Correct |
46 ms |
11820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
5 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
11260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
5716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |