# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
652850 | lovrot | Fireworks (APIO16_fireworks) | C++17 | 27 ms | 43236 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
struct func{
ll a;
ll b;
int ind;
};
const int N = 300010;
int n, m, siz[N];
ll edge[N];
vector<int> g[N];
multiset<ll> slope[N];
func merge(func a, func b){
func ret;
ret.a = a.a + b.a;
ret.b = a.b + b.b;
ret.ind = a.ind;
for(auto x : slope[b.ind]) slope[a.ind].insert(x);
return ret;
}
func f(int u){
func ret;
if(g[u].empty()){
ret.a = 1;
ret.b = -edge[u];
ret.ind = u;
slope[u].insert(edge[u]);
slope[u].insert(edge[u]);
return ret;
}
ret = f(g[u][0]);
for(int i = 1; i < g[u].size(); i++){
ret = merge(ret, f(g[u][i]));
}
int ind = ret.ind;
while(ret.a > 1){
ll last = *slope[ind].rbegin();
slope[ind].erase(prev(slope[ind].end()));
ret.b += last;
ret.a--;
}
ll val = *prev(slope[ind].end()) + edge[u];
slope[ind].erase(prev(slope[ind].end()));
ll val2 = *prev(slope[ind].end()) + edge[u];
slope[ind].erase(prev(slope[ind].end()));
ret.b -= edge[u];
slope[ind].insert(val);
slope[ind].insert(val2);
return ret;
}
int dfs(int u){
int ret = 1;
for(int v : g[u]){
ret += dfs(v);
}
siz[u] = ret;
return ret;
}
int main(){
scanf("%d%d\n", &n, &m);
for(int i = 2; i <= n + m; i++){
int par, val;
scanf("%d%d", &par, &val);
g[par].pb(i);
edge[i] = val;
}
dfs(1);
for(int i = 1; i <= n; i++){
sort(g[i].begin(), g[i].end(), [](int a, int b) -> bool {return siz[a] >= siz[b];});
}
func res = f(1);
ll ans = *slope[res.ind].rbegin() + res.b;
printf("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |