이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Mayoeba Yabureru
*/
#include<bits/stdc++.h>
using namespace std;
void solve() {
long long n, s, ans = 0;
cin >> n >> s;
vector<long long> x(n + 1);
vector<int> p(n + 1), st, did(n + 1);
vector<vector<int>> gr(n + 1);
for (int i = 1; i <= n; i ++) {
cin >> x[i] >> p[i];
if (p[i] == 0) st.push_back(i);
else gr[p[i]].push_back(i);
}
set<pair<pair<long long, long long>, pair<vector<int>, int>>> ss;
long long sum = 0, mn = 0, alsum = 0;
vector<int> vv;
function<void(int, int)> go = [&] (int v, int a) {
sum += x[v];
mn = min(mn, sum);
if (sum > 0) {
vv.push_back(v);
alsum += sum;
}
else {
for (auto u : gr[v]) {
go(u, a);
sum = 0;
}
}
};
function<void(int, int)> rgo = [&] (int v, int a) {
did[v] = 1;
for (auto u : gr[v]) {
if (did[u]) continue;
alsum = sum = mn = 0;
vv.clear();
go(u, u);
ss.insert({{mn, alsum}, {vv, u}});
}
if (a != v && did[p[v]] == 0) rgo(p[v], a);
};
for (auto v : st) {
alsum = sum = mn = 0;
vv.clear();
go(v, v);
ss.insert({{mn, alsum}, {vv, v}});
}
while (!ss.empty()) {
auto [x, vv] = *ss.begin();
ss.erase(ss.begin());
long long mnn = x.first, summ = x.second;
vector<int> vvv = vv.first;
int a = vv.second;
if (mnn <= s && did[a] == 0) {
s += summ, ans += summ;
for (auto v : vvv) rgo(v, a);
}
}
cout << ans << endl;
}
/*
6 1
-1 0
2 1
3 2
-10 0
1 4
-2 5
*/
int main()
{
int T = 1;
for (int I = 0; I < T; I ++){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void solve()':
Main.cpp:55:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | auto [x, vv] = *ss.begin();
| ^
# | 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... |