이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 3e5 + 5;
typedef long long ll;
ll A[Nmax], B[Nmax];
int up[Nmax], w[Nmax];
priority_queue<ll> heap[Nmax];
vector<int> edge[Nmax];
int n, m, N;
void combine(int node, int son)
{
A[node] += A[son]; B[node] += B[son];
while(heap[son].size())
{
heap[node].push(heap[son].top());
heap[son].pop();
}
}
void merge_sons(int node)
{
int xson = 0;
for(auto son : edge[node])
{
if(w[son] > w[xson]) xson = son;
w[node] += w[son];
}
swap(heap[node], heap[xson]);
A[node] = A[xson]; B[node] = B[xson];
for(auto son : edge[node])
if(son != xson)
combine(node, son);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
N = n + m;
int i, node;
for(i=2; i<=N; ++i)
{
int parent;
cin >> parent >> up[i];
edge[parent].push_back(i);
}
for(node = N; node; --node)
{
w[node] = 1;
if(edge[node].empty())
{
heap[node].push(up[node]);
heap[node].push(up[node]);
A[node] = 1; B[node] = -up[node];
continue;
}
merge_sons(node);
while(A[node] > 1)
{
ll last_change = heap[node].top();
heap[node].pop();
A[node]--; B[node] += last_change;
}
ll change0, change1;
change1 = heap[node].top(); heap[node].pop();
change0 = heap[node].top(); heap[node].pop();
heap[node].push(change1 + up[node]);
heap[node].push(change0 + up[node]);
B[node] -= up[node];
}
cout << A[1] * heap[1].top() + B[1] << '\n';
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... |