This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |