#include <random>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#define fr first
#define sc second
#define ad push_back
using namespace std;
using ll = long long;
mt19937 rnd(348502);
const ll N = 1000005;
vector<ll> v[N];
pair<ll, ll> a[N];
ll dp[2000][2000], k;
int pereadres[N];
int popopopox = 1;
int gagperead[N];
vector<int> taza[N];
///
ll dp1[N][21];
void Dfs1(ll g)
{
if (a[g].first)
dp1[g][a[g].first - 1] = a[g].second;
for (auto to : v[g])
{
Dfs1(to);
for (ll i = 0; i < k; i++)
{
dp1[g][i] += dp1[to][i];
}
}
for (ll i = 1; i < k; i++)
{
dp1[g][i] = max(dp1[g][i], dp1[g][i - 1]);
}
}
///
void Dfs(ll g)
{
if(a[g].first)
dp[gagperead[g]][pereadres[a[g].first]] = a[g].second;
for (auto to : taza[gagperead[g]])
{
Dfs(to);
for (ll i = 0; i < popopopox; i++)
{
dp[gagperead[g]][i] += dp[gagperead[to]][i];
}
}
for (ll i = 1; i < popopopox; i++)
{
dp[gagperead[g]][i] = max(dp[gagperead[g]][i], dp[gagperead[g]][i - 1]);
}
}
ll s[4 * N];
ll get_max(int p, int lp, int rp, int l, int r)
{
if (l > r)return 0;
if (lp == l && rp == r)
return s[p];
int m = (lp + rp) / 2;
return max(get_max(p * 2, lp, m, l, min(m, r)), get_max(p * 2 + 1, m + 1, rp, max(m + 1, l), r));
}
void update(int p, int lp, int rp , int ind, ll arj)
{
if (lp == rp)
{
s[p] = arj;
return;
}
int m = (lp + rp) / 2;
if (ind <= m)
update(p * 2, lp, m, ind, arj);
else
update(p * 2 + 1, m + 1, rp, ind, arj);
s[p] = max(s[p * 2], s[p * 2 + 1]);
}
int neeew = 1;
void Dfspereadres(int g, int naxk)
{
if (a[g].first != 0 || g == 1)
{
gagperead[g] = neeew++;
}
if (a[g].first != 0 && naxk != g)
{
taza[naxk].push_back(g);
}
for (auto to : v[g])
{
if (a[g].first == 0)
Dfspereadres(to, naxk);
else
Dfspereadres(to, gagperead[g]);
}
}
int kachka[N];
void solve()
{
ll n, i, j, m, x, y, z;
cin >> n >> m >> k;
if (m <= 1000)
{
for (i = 0; i < n - 1; i++)
{
cin >> x;
v[x].push_back(i + 2);
}
for (i = 0; i < m; i++)
{
cin >> x >> y >> z;
kachka[y] = 1;
a[x] = { y, z };
}
for ( i = 1; i <= k; i++)
{
if (kachka[i] == 1)
{
pereadres[i] = popopopox++;
}
}
Dfspereadres(1, 1);
Dfs(1);
cout << dp[1][popopopox - 1];
return;
}
if (k <= 20)
{
for (i = 0; i < n - 1; i++)
{
cin >> x;
v[x].push_back(i + 2);
}
for (i = 0; i < m; i++)
{
cin >> x >> y >> z;
a[x] = { y, z };
}
Dfs1(1);
cout << dp1[1][k - 1];
return;
}
ll ans = 0;
int st = 1;
for (i = 0; i < n - 1; i++)
{
cin >> x;
if (x != i + 1)
st = 0;
v[x].push_back(i + 2);
}
for (i = 0; i < m; i++)
{
cin >> x >> y >> z;
a[x] = { y, z };
ans += z;
}
if (st == 0)
{
cout << ans;
return;
}
for (i = n; i > 0; i--)
{
if (a[i].first)
update(1, 0, k, a[i].first, a[i].second + get_max(1, 0, k, 0, a[i].first));
}
cout << get_max(1, 0, k, 0, k);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
return 0;
}
Compilation message
magictree.cpp: In function 'void solve()':
magictree.cpp:130:14: warning: unused variable 'j' [-Wunused-variable]
130 | ll n, i, j, m, x, y, z;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
55388 KB |
Output is correct |
2 |
Correct |
16 ms |
55388 KB |
Output is correct |
3 |
Correct |
17 ms |
55388 KB |
Output is correct |
4 |
Correct |
16 ms |
55388 KB |
Output is correct |
5 |
Correct |
16 ms |
55388 KB |
Output is correct |
6 |
Correct |
16 ms |
55388 KB |
Output is correct |
7 |
Correct |
16 ms |
55388 KB |
Output is correct |
8 |
Correct |
16 ms |
55388 KB |
Output is correct |
9 |
Correct |
17 ms |
55388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
58196 KB |
Output is correct |
2 |
Correct |
30 ms |
56788 KB |
Output is correct |
3 |
Correct |
45 ms |
58964 KB |
Output is correct |
4 |
Correct |
37 ms |
58232 KB |
Output is correct |
5 |
Correct |
42 ms |
59208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
63832 KB |
Output is correct |
2 |
Correct |
18 ms |
63580 KB |
Output is correct |
3 |
Correct |
18 ms |
63580 KB |
Output is correct |
4 |
Correct |
59 ms |
62804 KB |
Output is correct |
5 |
Correct |
59 ms |
64084 KB |
Output is correct |
6 |
Correct |
70 ms |
62812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
75860 KB |
Output is correct |
2 |
Correct |
62 ms |
77652 KB |
Output is correct |
3 |
Correct |
60 ms |
81492 KB |
Output is correct |
4 |
Correct |
41 ms |
76668 KB |
Output is correct |
5 |
Correct |
52 ms |
86868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
55388 KB |
Output is correct |
2 |
Correct |
16 ms |
55388 KB |
Output is correct |
3 |
Correct |
17 ms |
55388 KB |
Output is correct |
4 |
Correct |
16 ms |
55388 KB |
Output is correct |
5 |
Correct |
16 ms |
55388 KB |
Output is correct |
6 |
Correct |
16 ms |
55388 KB |
Output is correct |
7 |
Correct |
16 ms |
55388 KB |
Output is correct |
8 |
Correct |
16 ms |
55388 KB |
Output is correct |
9 |
Correct |
17 ms |
55388 KB |
Output is correct |
10 |
Correct |
69 ms |
77140 KB |
Output is correct |
11 |
Correct |
67 ms |
77140 KB |
Output is correct |
12 |
Correct |
66 ms |
80884 KB |
Output is correct |
13 |
Correct |
40 ms |
73936 KB |
Output is correct |
14 |
Correct |
61 ms |
86100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
70236 KB |
Output is correct |
2 |
Correct |
38 ms |
74324 KB |
Output is correct |
3 |
Correct |
46 ms |
74276 KB |
Output is correct |
4 |
Correct |
40 ms |
78000 KB |
Output is correct |
5 |
Correct |
26 ms |
76496 KB |
Output is correct |
6 |
Correct |
36 ms |
79452 KB |
Output is correct |
7 |
Correct |
36 ms |
81748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
55388 KB |
Output is correct |
2 |
Correct |
16 ms |
55388 KB |
Output is correct |
3 |
Correct |
17 ms |
55388 KB |
Output is correct |
4 |
Correct |
16 ms |
55388 KB |
Output is correct |
5 |
Correct |
16 ms |
55388 KB |
Output is correct |
6 |
Correct |
16 ms |
55388 KB |
Output is correct |
7 |
Correct |
16 ms |
55388 KB |
Output is correct |
8 |
Correct |
16 ms |
55388 KB |
Output is correct |
9 |
Correct |
17 ms |
55388 KB |
Output is correct |
10 |
Correct |
18 ms |
63832 KB |
Output is correct |
11 |
Correct |
18 ms |
63580 KB |
Output is correct |
12 |
Correct |
18 ms |
63580 KB |
Output is correct |
13 |
Correct |
59 ms |
62804 KB |
Output is correct |
14 |
Correct |
59 ms |
64084 KB |
Output is correct |
15 |
Correct |
70 ms |
62812 KB |
Output is correct |
16 |
Correct |
69 ms |
77140 KB |
Output is correct |
17 |
Correct |
67 ms |
77140 KB |
Output is correct |
18 |
Correct |
66 ms |
80884 KB |
Output is correct |
19 |
Correct |
40 ms |
73936 KB |
Output is correct |
20 |
Correct |
61 ms |
86100 KB |
Output is correct |
21 |
Incorrect |
21 ms |
53852 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
55388 KB |
Output is correct |
2 |
Correct |
16 ms |
55388 KB |
Output is correct |
3 |
Correct |
17 ms |
55388 KB |
Output is correct |
4 |
Correct |
16 ms |
55388 KB |
Output is correct |
5 |
Correct |
16 ms |
55388 KB |
Output is correct |
6 |
Correct |
16 ms |
55388 KB |
Output is correct |
7 |
Correct |
16 ms |
55388 KB |
Output is correct |
8 |
Correct |
16 ms |
55388 KB |
Output is correct |
9 |
Correct |
17 ms |
55388 KB |
Output is correct |
10 |
Correct |
35 ms |
58196 KB |
Output is correct |
11 |
Correct |
30 ms |
56788 KB |
Output is correct |
12 |
Correct |
45 ms |
58964 KB |
Output is correct |
13 |
Correct |
37 ms |
58232 KB |
Output is correct |
14 |
Correct |
42 ms |
59208 KB |
Output is correct |
15 |
Correct |
18 ms |
63832 KB |
Output is correct |
16 |
Correct |
18 ms |
63580 KB |
Output is correct |
17 |
Correct |
18 ms |
63580 KB |
Output is correct |
18 |
Correct |
59 ms |
62804 KB |
Output is correct |
19 |
Correct |
59 ms |
64084 KB |
Output is correct |
20 |
Correct |
70 ms |
62812 KB |
Output is correct |
21 |
Correct |
60 ms |
75860 KB |
Output is correct |
22 |
Correct |
62 ms |
77652 KB |
Output is correct |
23 |
Correct |
60 ms |
81492 KB |
Output is correct |
24 |
Correct |
41 ms |
76668 KB |
Output is correct |
25 |
Correct |
52 ms |
86868 KB |
Output is correct |
26 |
Correct |
69 ms |
77140 KB |
Output is correct |
27 |
Correct |
67 ms |
77140 KB |
Output is correct |
28 |
Correct |
66 ms |
80884 KB |
Output is correct |
29 |
Correct |
40 ms |
73936 KB |
Output is correct |
30 |
Correct |
61 ms |
86100 KB |
Output is correct |
31 |
Correct |
23 ms |
70236 KB |
Output is correct |
32 |
Correct |
38 ms |
74324 KB |
Output is correct |
33 |
Correct |
46 ms |
74276 KB |
Output is correct |
34 |
Correct |
40 ms |
78000 KB |
Output is correct |
35 |
Correct |
26 ms |
76496 KB |
Output is correct |
36 |
Correct |
36 ms |
79452 KB |
Output is correct |
37 |
Correct |
36 ms |
81748 KB |
Output is correct |
38 |
Incorrect |
21 ms |
53852 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |