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 <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define dbg(x) cout << "reached here " << x << endl;
#define ll long long
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5+5;
int a[maxn];
vector<int> adj[maxn];
map<int, int> sack[maxn];
void merge(int v, int u)
{
for (auto e: sack[u])
sack[v][e.f] += e.s;
}
void dfs(int v)
{
int ptr = v;
for(auto u: adj[v])
{
dfs(u);
if(sack[u].size() > sack[v].size())
{
swap(sack[u], sack[v]);
ptr = u;
}
}
for (auto u: adj[v])
if(u != ptr)
merge(v, u);
if(a[v])
{
int h = 0;
auto it = sack[v].rbegin();
if(sack[v].size() > 1)
{
if((*it).f > a[v])
h += (*it).s;
it--;
if((*it).f > a[v])
h += (*it).s;
}
else if(sack[v].size() == 1)
if((*it).f > a[v])
h += (*it).s;
if(h <= 1)
{
sack[v][a[v]]++;
auto it2 = sack[v].rbegin();
while((*it2).f > a[v] and it2 != sack[v].rend())
{
auto del = sack[v].find((*it2).f);
sack[v].erase(del);
it2--;
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 2; i <= n; ++i)
{
int p;
cin >> p;
adj[p].pb(i);
}
for (int i = 1; i <= m; ++i)
{
int v, d, w;
cin >> v >> d >> w;
a[v] = d;
}
dfs(1);
int ans = 0;
for (auto u: sack[1])
ans += u.s;
cout << ans << endl;
return 0;
}
# | 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... |