// nén cây và k dựa vào m
// O(n * min(m, k))
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define ff first
#define ss second
const int N = 1e5 + 9;
int n, m, k;
int p[N];
pii fruit[N];
vector<int> g[N];
namespace sub2 {
bool check() {
for (int i = 2; i <= n; ++i)
if (g[i].size() && fruit[i].ff) return false;
return true;
}
void solve() {
ll sum(0);
for (int i = 2; i <= n; ++i)
sum += fruit[i].ss;
cout << sum << '\n';
}
}
namespace sub3 {
bool check() {
for (int i = 2; i <= n; ++i)
if (p[i] != i - 1 || fruit[i].ff > 1) return false;
return true;
}
int lis[N];
void solve() {
int cnt(0);
for (int i = n, pos; i; --i) if (fruit[i].ff) {
pos = lower_bound(lis + 1, lis + cnt + 1, fruit[i].ff) - lis;
if (cnt < pos) cnt = pos;
lis[pos] = fruit[i].ff;
}
cout << cnt << '\n';
}
}
int dp[N][1003];
namespace sub1456 {
int cnt(0), b[N];
void dfs(int u) {
for (int v : g[u]) {
dfs(v);
for (int i = 0; i < cnt; ++i)
dp[u][i] += dp[v][i];
}
if (fruit[u].ff) {
dp[u][fruit[u].ff] += fruit[u].ss;
for (int i = fruit[u].ff + 1; i < cnt; ++i)
dp[u][i] = max(dp[u][i], fruit[u].ss);
}
}
void solve() {
for (int i = 1; i <= n; ++i) if (fruit[i].ff) b[++cnt] = fruit[i].ff;
sort(b + 1, b + cnt + 1);
cnt = unique(b + 1, b + cnt + 1) - b;
for (int i = 1; i <= n; ++i)
if (fruit[i].ff) fruit[i].ff = lower_bound(b + 1, b + cnt, fruit[i].ff) - b;
dfs(1);
cout << *max_element(dp[1], dp[1] + cnt) << '\n';
}
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = 2; i <= n; ++i) {
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i = 0, v; i < m; ++i) {
cin >> v;
cin >> fruit[v].ff >> fruit[v].ss;
}
if (sub2::check()) sub2::solve();
else if (sub3::check()) sub3::solve();
else sub1456::solve();
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
6332 KB |
Output is correct |
2 |
Correct |
20 ms |
6636 KB |
Output is correct |
3 |
Correct |
34 ms |
6976 KB |
Output is correct |
4 |
Correct |
29 ms |
6528 KB |
Output is correct |
5 |
Correct |
33 ms |
6916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6732 KB |
Output is correct |
2 |
Incorrect |
5 ms |
6732 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
214 ms |
381156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
54740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |