// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, m, k; vector<int> g[MAXN];
ii c[MAXN];
map<int, ll> mp[MAXN]; bool ok[MAXN];
void dfs(int u, int par){
Forv(v, g[u]) if(v ^ par){
dfs(v, u);
if(mp[u].size() < mp[v].size()) swap(mp[u], mp[v]);
Forv(tmp, mp[v]) mp[u][tmp.fi] += tmp.se;
mp[v].clear();
}
if (ok[u]){
auto [day, w] = c[u];
mp[u][day] += w;
auto it = mp[u].upper_bound(day);
while (it != mp[u].end()){
if (it->se > w){ it->se -= w; break; }
auto cur = it; ++it;
w -= cur -> se;
mp[u].erase(cur);
}
}
}
void Solve() {
cin >> n >> m >> k;
For(i, 2, n){
int p; cin >> p; g[p].push_back(i);
}
For(i, 1, m){
int node, day, w;
cin >> node >> day >> w;
c[node] = {day, w};
ok[node] = true;
}
dfs(1, 0);
ll ans = 0;
for(auto [_, w] : mp[1]) ans += w;
cout << ans << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("CEOI19_magictree.inp", "r")) {
freopen("CEOI19_magictree.inp", "r", stdin);
freopen("CEOI19_magictree.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++) {
Solve();
}
return 0;
}
Compilation message
magictree.cpp: In function 'int32_t main()':
magictree.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen("CEOI19_magictree.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("CEOI19_magictree.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
73552 KB |
Output is correct |
2 |
Correct |
17 ms |
73460 KB |
Output is correct |
3 |
Correct |
22 ms |
73552 KB |
Output is correct |
4 |
Correct |
25 ms |
73544 KB |
Output is correct |
5 |
Correct |
28 ms |
73544 KB |
Output is correct |
6 |
Correct |
30 ms |
73552 KB |
Output is correct |
7 |
Correct |
34 ms |
73544 KB |
Output is correct |
8 |
Correct |
29 ms |
73348 KB |
Output is correct |
9 |
Correct |
29 ms |
73552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
78484 KB |
Output is correct |
2 |
Correct |
63 ms |
83784 KB |
Output is correct |
3 |
Correct |
119 ms |
80572 KB |
Output is correct |
4 |
Correct |
90 ms |
79100 KB |
Output is correct |
5 |
Correct |
90 ms |
80944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73544 KB |
Output is correct |
2 |
Correct |
26 ms |
73552 KB |
Output is correct |
3 |
Correct |
25 ms |
73576 KB |
Output is correct |
4 |
Correct |
72 ms |
92232 KB |
Output is correct |
5 |
Correct |
93 ms |
96184 KB |
Output is correct |
6 |
Correct |
86 ms |
92484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
76612 KB |
Output is correct |
2 |
Correct |
68 ms |
76764 KB |
Output is correct |
3 |
Correct |
63 ms |
83272 KB |
Output is correct |
4 |
Correct |
49 ms |
75720 KB |
Output is correct |
5 |
Correct |
79 ms |
92232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
73552 KB |
Output is correct |
2 |
Correct |
17 ms |
73460 KB |
Output is correct |
3 |
Correct |
22 ms |
73552 KB |
Output is correct |
4 |
Correct |
25 ms |
73544 KB |
Output is correct |
5 |
Correct |
28 ms |
73544 KB |
Output is correct |
6 |
Correct |
30 ms |
73552 KB |
Output is correct |
7 |
Correct |
34 ms |
73544 KB |
Output is correct |
8 |
Correct |
29 ms |
73348 KB |
Output is correct |
9 |
Correct |
29 ms |
73552 KB |
Output is correct |
10 |
Correct |
72 ms |
76880 KB |
Output is correct |
11 |
Correct |
73 ms |
76616 KB |
Output is correct |
12 |
Correct |
63 ms |
83272 KB |
Output is correct |
13 |
Correct |
54 ms |
75728 KB |
Output is correct |
14 |
Correct |
77 ms |
94280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
74056 KB |
Output is correct |
2 |
Correct |
54 ms |
77128 KB |
Output is correct |
3 |
Correct |
68 ms |
77136 KB |
Output is correct |
4 |
Correct |
59 ms |
77404 KB |
Output is correct |
5 |
Correct |
40 ms |
75720 KB |
Output is correct |
6 |
Correct |
51 ms |
79688 KB |
Output is correct |
7 |
Correct |
47 ms |
83784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
73552 KB |
Output is correct |
2 |
Correct |
17 ms |
73460 KB |
Output is correct |
3 |
Correct |
22 ms |
73552 KB |
Output is correct |
4 |
Correct |
25 ms |
73544 KB |
Output is correct |
5 |
Correct |
28 ms |
73544 KB |
Output is correct |
6 |
Correct |
30 ms |
73552 KB |
Output is correct |
7 |
Correct |
34 ms |
73544 KB |
Output is correct |
8 |
Correct |
29 ms |
73348 KB |
Output is correct |
9 |
Correct |
29 ms |
73552 KB |
Output is correct |
10 |
Correct |
28 ms |
73544 KB |
Output is correct |
11 |
Correct |
26 ms |
73552 KB |
Output is correct |
12 |
Correct |
25 ms |
73576 KB |
Output is correct |
13 |
Correct |
72 ms |
92232 KB |
Output is correct |
14 |
Correct |
93 ms |
96184 KB |
Output is correct |
15 |
Correct |
86 ms |
92484 KB |
Output is correct |
16 |
Correct |
72 ms |
76880 KB |
Output is correct |
17 |
Correct |
73 ms |
76616 KB |
Output is correct |
18 |
Correct |
63 ms |
83272 KB |
Output is correct |
19 |
Correct |
54 ms |
75728 KB |
Output is correct |
20 |
Correct |
77 ms |
94280 KB |
Output is correct |
21 |
Correct |
37 ms |
74320 KB |
Output is correct |
22 |
Correct |
68 ms |
77192 KB |
Output is correct |
23 |
Correct |
76 ms |
78916 KB |
Output is correct |
24 |
Correct |
101 ms |
80064 KB |
Output is correct |
25 |
Correct |
68 ms |
79304 KB |
Output is correct |
26 |
Correct |
82 ms |
83224 KB |
Output is correct |
27 |
Correct |
80 ms |
84848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
73552 KB |
Output is correct |
2 |
Correct |
17 ms |
73460 KB |
Output is correct |
3 |
Correct |
22 ms |
73552 KB |
Output is correct |
4 |
Correct |
25 ms |
73544 KB |
Output is correct |
5 |
Correct |
28 ms |
73544 KB |
Output is correct |
6 |
Correct |
30 ms |
73552 KB |
Output is correct |
7 |
Correct |
34 ms |
73544 KB |
Output is correct |
8 |
Correct |
29 ms |
73348 KB |
Output is correct |
9 |
Correct |
29 ms |
73552 KB |
Output is correct |
10 |
Correct |
74 ms |
78484 KB |
Output is correct |
11 |
Correct |
63 ms |
83784 KB |
Output is correct |
12 |
Correct |
119 ms |
80572 KB |
Output is correct |
13 |
Correct |
90 ms |
79100 KB |
Output is correct |
14 |
Correct |
90 ms |
80944 KB |
Output is correct |
15 |
Correct |
28 ms |
73544 KB |
Output is correct |
16 |
Correct |
26 ms |
73552 KB |
Output is correct |
17 |
Correct |
25 ms |
73576 KB |
Output is correct |
18 |
Correct |
72 ms |
92232 KB |
Output is correct |
19 |
Correct |
93 ms |
96184 KB |
Output is correct |
20 |
Correct |
86 ms |
92484 KB |
Output is correct |
21 |
Correct |
77 ms |
76612 KB |
Output is correct |
22 |
Correct |
68 ms |
76764 KB |
Output is correct |
23 |
Correct |
63 ms |
83272 KB |
Output is correct |
24 |
Correct |
49 ms |
75720 KB |
Output is correct |
25 |
Correct |
79 ms |
92232 KB |
Output is correct |
26 |
Correct |
72 ms |
76880 KB |
Output is correct |
27 |
Correct |
73 ms |
76616 KB |
Output is correct |
28 |
Correct |
63 ms |
83272 KB |
Output is correct |
29 |
Correct |
54 ms |
75728 KB |
Output is correct |
30 |
Correct |
77 ms |
94280 KB |
Output is correct |
31 |
Correct |
35 ms |
74056 KB |
Output is correct |
32 |
Correct |
54 ms |
77128 KB |
Output is correct |
33 |
Correct |
68 ms |
77136 KB |
Output is correct |
34 |
Correct |
59 ms |
77404 KB |
Output is correct |
35 |
Correct |
40 ms |
75720 KB |
Output is correct |
36 |
Correct |
51 ms |
79688 KB |
Output is correct |
37 |
Correct |
47 ms |
83784 KB |
Output is correct |
38 |
Correct |
37 ms |
74320 KB |
Output is correct |
39 |
Correct |
68 ms |
77192 KB |
Output is correct |
40 |
Correct |
76 ms |
78916 KB |
Output is correct |
41 |
Correct |
101 ms |
80064 KB |
Output is correct |
42 |
Correct |
68 ms |
79304 KB |
Output is correct |
43 |
Correct |
82 ms |
83224 KB |
Output is correct |
44 |
Correct |
80 ms |
84848 KB |
Output is correct |
45 |
Correct |
43 ms |
74832 KB |
Output is correct |
46 |
Correct |
78 ms |
78628 KB |
Output is correct |
47 |
Correct |
87 ms |
80716 KB |
Output is correct |
48 |
Correct |
109 ms |
82760 KB |
Output is correct |
49 |
Correct |
81 ms |
81352 KB |
Output is correct |
50 |
Correct |
90 ms |
86088 KB |
Output is correct |
51 |
Correct |
98 ms |
87464 KB |
Output is correct |