// 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].ss > 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 = upper_bound(lis + 1, lis + cnt + 1, fruit[i].ff) - lis;
if (cnt < pos) cnt = pos;
lis[pos] = fruit[i].ff;
}
cout << cnt << '\n';
}
}
ll 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], (ll)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 |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
6236 KB |
Output is correct |
2 |
Correct |
25 ms |
6604 KB |
Output is correct |
3 |
Correct |
38 ms |
6904 KB |
Output is correct |
4 |
Correct |
28 ms |
6416 KB |
Output is correct |
5 |
Correct |
35 ms |
6984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2692 KB |
Output is correct |
4 |
Correct |
32 ms |
8876 KB |
Output is correct |
5 |
Correct |
40 ms |
9328 KB |
Output is correct |
6 |
Correct |
36 ms |
8888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
203 ms |
388244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
92080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |