이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> v2i;
typedef pair<int, int> pi;
typedef vector<ll> vl;
typedef vector<vl> v2l;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
//sub 1
vl p;
v2i adj;
vi par;
vl tp;
vb done;
vl req;
int nextjob;
int njd; // next job depth
ll m; // current money
void dfsu(int x, ll ctp=0, ll creq = 0, ll pmr=0, int depth=0) {
// use dfs to update the current node
// tp: the total profit gained if this job is done
// req: the total money requirement of going to this job
// pmr: the total positive money reserve
tp[x] = ctp;
req[x] = creq;
if (creq < m && ctp > 0 && depth > njd) {
nextjob = x;
njd = depth;
}
//cout << "Jobs upto job " << x << " can now be done with profit " << ctp << " and require " << creq << "\n";
for (auto a : adj[x]) {
if (!done[a]) {
if (pmr + p[a] < 0) dfsu(a, ctp + p[a], creq - (pmr + p[a]), 0, depth + 1);
else dfsu(a, ctp + p[a], creq, pmr + p[a], depth + 1);
}
else {
dfsu(a);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
ll s;
cin >> n >> s;
//if (s < 10e17) return 1;
p.pb(s);
par.pb(-1);
adj.resize(n + 1);
done.resize(n + 1, false); done[0] = true;
tp.resize(n + 1);
req.resize(n + 1);
par.resize(n + 1, 0);
ll x, p_i;
rep(i, n) {
cin >> x >> p_i;
adj[p_i].pb(i + 1);
par[i + 1] = p_i;
p.pb(x);
}
m = s;
nextjob = -1;
njd = 0;
dfsu(0);
while (nextjob != -1) {
int j = nextjob;
//cout << "Jobs upto job " << j << " were done with profit " << tp[j] << "\n";
while (!done[j]) {
done[j] = true;
j = par[j];
}
m += tp[nextjob];
//cout << "Current money: " << m << "-------------------------------------------------------------\n";
nextjob = -1;
njd = 0;
dfsu(0);
}
cout << m - s;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |