# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
154114 | junhopark | Fireworks (APIO16_fireworks) | C++14 | 403 ms | 66044 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define eb emplace_back
#define all(V) (V).begin(),(V).end()
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int SZ = 3e5+5;
int n, m, piv, ch[SZ], si[SZ];
int pl[SZ], l[SZ], sub[SZ];
LL dep[SZ], sum;
priority_queue<LL> pq[SZ];
vector<pii> v[SZ];
vector<LL> res, test;
void get_dep(int now)
{
ch[now]=sub[now]=1;
for (pii c:v[now]) {
dep[c.fi]=dep[now]+1;
get_dep(c.fi);
sub[now]+=sub[c.fi];
}
}
void solve(int now)
{
if (now>n) {
pl[now]=++piv;
pq[piv].emplace(l[now]);
pq[piv].emplace(l[now]);
si[piv]=-1;
return;
}
LL len;
int p, maxi=-1;
for (pii c:v[now]) {
solve(c.fi);
if (maxi<(int)pq[pl[c.fi]].size()) {
maxi=(int)pq[pl[c.fi]].size();
p=c.fi;
len=l[c.fi];
}
}
pl[now]=pl[p];
for (pii c:v[now]) {
if (c.fi==p) continue;
si[pl[now]]+=si[pl[c.fi]];
while (pq[pl[c.fi]].size()) {
pq[pl[now]].emplace(pq[pl[c.fi]].top());
pq[pl[c.fi]].pop();
}
}
if (now>1) {
LL cnt=si[pl[now]]+(int)pq[pl[now]].size()-1;
while (cnt--) pq[pl[now]].pop();
LL a, b, c;
a=pq[pl[now]].top();
pq[pl[now]].pop();
b=pq[pl[now]].top();
pq[pl[now]].pop();
c=pq[pl[now]].top();
a+=l[now],b+=l[now];
pq[pl[now]].emplace(a);
pq[pl[now]].emplace(b);
}
}
int main()
{
int i, a, b;
scanf("%d %d", &n, &m);
for (i=2; i<=n+m; i++) {
scanf("%d %d", &a, &b);
v[a].eb(i,b);
l[i]=b;
sum+=b;
}
get_dep(1);
solve(1);
while (pq[pl[1]].size()) {
res.eb(pq[pl[1]].top());
pq[pl[1]].pop();
}
LL prev=0, pp=res.size()-1;
while (si[pl[1]]<0) {
sum+=(res[pp]-prev)*si[pl[1]];
si[pl[1]]++;
prev=res[pp--];
}
printf("%lld\n", sum);
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... |