이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <bitset>
#include <unordered_map>
#include <complex>
#include <unordered_set>
#include <chrono>
#include <random>
#include <array>
#include <functional>
#include <random>
///1:54
using namespace std;
const int maxN = 300009;
int N, M, t[maxN], d[maxN], sz[maxN];
vector<int> v[maxN];
void dfsSz(int nod) {
int pos = 0, heaviest = -1;
sz[nod] = 1;
for (auto it : v[nod]) {
dfsSz(it);
sz[nod] += sz[it];
if (heaviest == -1 || sz[it] > sz[v[nod][heaviest]])
heaviest = pos;
pos ++;
}
if (heaviest > 0)
swap(v[nod][heaviest], v[nod][0]);
}
struct dpFunction {
multiset<long long> S;
long long platformLength = 0, smallest = 0;
long long platformStart() {
if (S.empty()) return 0;
return *S.rbegin();
}
void addD(int d) {
if (S.empty()) {
S.insert(d);
return ;
}
auto it = S.end(); it --;
S.insert(*it + d);
S.erase(it);
}
void operator += (const dpFunction& other) {
for (auto it : other.S)
S.insert(it);
smallest += other.smallest;
}
void print() {
printf("{");
for (auto it : S)
printf("%lld,", it);
printf("\b} min=%lld, platform=%lld", smallest, platformLength);
}
}dp[20];
void solve(int nod, int lev) {
if (v[nod].empty()) {
dp[lev] = dpFunction();
return ;
}
vector<long long> rightEnds;
for (auto it : v[nod]) {
if (it != v[nod][0]) {
dp[lev + 1] = dpFunction();
solve(it, lev + 1);
dp[lev + 1].addD(d[it]);
dp[lev] += dp[lev + 1];
rightEnds.push_back(dp[lev + 1].platformStart() + dp[lev + 1].platformLength);
} else {
solve(it, lev);
dp[lev].addD(d[it]);
rightEnds.push_back(dp[lev].platformStart() + dp[lev].platformLength);
}
}
/*printf("rightEnds:[");
for (auto it : rightEnds)
printf("%lld ", it);
printf("\b]\n");*/
//sort(rightEnds.begin(), rightEnds.end());
for (auto x : rightEnds) {
dp[lev].S.insert(x);
//dp[lev].print(), printf("following %d\n", x);
auto it2 = dp[lev].S.end(); it2 --;
auto it = it2; it --;
dp[lev].platformLength = *it2 - *it;
dp[lev].smallest += *it2 - x;
dp[lev].S.erase(it2);
}
//printf("%d ", nod), dp[lev].print(), printf("\n");
}
int main() {
//freopen("../input", "r", stdin);
//freopen("../output", "w", stdout);
scanf("%d %d", &N, &M), N += M;
for (int i=2; i<=N; i++) {
scanf("%d %d", &t[i], &d[i]);
v[t[i]].push_back(i);
}
dfsSz(1);
solve(1, 0);
printf("%lld\n", dp[0].smallest);
return 0;
}
/*const int seed = time (0);
mt19937 gen (seed);
long long getRand(long long a, long long b) {return uniform_int_distribution < long long > (a, b) (gen);}*/
컴파일 시 표준 에러 (stderr) 메시지
fireworks.cpp: In function 'int main()':
fireworks.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | scanf("%d %d", &N, &M), N += M;
| ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d %d", &t[i], &d[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |