This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 10;
const int inf1 = 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxn = 3e5+10;
int n, s, x[maxn], p[maxn], sm[maxn], pf[maxn], ds[maxn], dsz[maxn];
set<pair<int,int>> g[maxn];
priority_queue<pair<int,pair<int,int>>> pq;
int find(int u) {
if(u == -1) return -1;
if(ds[u] == u) return u;
return ds[u] = find(ds[u]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if(dsz[u] < dsz[v]) swap(u,v);
if(g[u].size() < g[v].size()) swap(g[u],g[v]);
dsz[u]+= dsz[v];
ds[v] = u;
for(auto x : g[v]) {
g[u].insert(x);
}
return;
}
void solve() {
cin >> n >> s;
for(int i = 1; i <= n; i++) {
cin >> x[i] >> p[i];
sm[i] = x[i];
pf[i] = x[i];
ds[i] = i;
dsz[i] = 1;
}
sm[0] = s;
pf[0] = 0;
ds[0] = 0;
dsz[0] = 1;
p[0] = -1;
x[0] = 0;
for(int i = 1; i <= n; i++) {
if(sm[i] >= 0) g[p[i]].insert(mp(pf[i],i));
}
for(int v = 1; v <= n; v++) {
int u = p[v];
if(sm[v] >= 0) pq.push(mp(min(pf[u],sm[u]+pf[v]),mp(u,v)));
}
while(pq.size()) {
int v = find(pq.top().sc.sc);
int u = find(pq.top().sc.fr);
pq.pop();
if(u != v && sm[v] >= 0 && -min(pf[u],sm[u]+pf[v]) <= sm[find(0)]) {
int newpf = min(pf[u],sm[u]+pf[v]);
int newsm = sm[u]+sm[v];
int newp = p[u];
join(u,v);
u = find(u);
pf[u] = newpf;
sm[u] = newsm;
p[u] = find(newp);
if(sm[u] >= 0 && p[u] != -1) {
pq.push(mp(min(pf[p[u]],sm[p[u]]+pf[u]),mp(p[u],u)));
}
if(p[u] != -1) {
g[p[u]].insert(mp(pf[u],u));
}
}
while(g[u].size()) {
v = find(g[u].begin()->sc);
g[u].erase(g[u].begin());
if(sm[v] >= 0) {
pq.push(mp(min(pf[u],sm[u]+pf[v]),mp(u,v)));
break;
}
}
}
// queue<pair<int,int>> q;
// for(int u = 0; u <= n; u++) {
// if(-pf[u] <= sm[find(0)]) {
// while(g[u].size() && -(sm[u]+g[u].begin()->fr) <= sm[find(0)]) {
// int v = find(g[u].begin()->sc);
// g[u].erase(g[u].begin());
// if(find(u) != find(v) && sm[v] >= 0 && -min(pf[u],sm[u]+pf[v]) <= sm[find(0)]) {
// q.push(mp(u,v));
// }
// }
// }
// }
// while(q.size()) {
// int u = find(q.front().fr);
// int v = find(q.front().sc);
// q.pop();
// if(find(u) != find(v) && sm[v] >= 0 && -min(pf[u],sm[u]+pf[v]) <= sm[find(0)]) {
// int newsm = sm[u]+sm[v];
// int newpf = min(pf[u],sm[u]+pf[v]);
// int newp = p[u];
// join(u,v);
// u = find(u);
// sm[u] = newsm;
// pf[u] = newpf;
// p[u] = newp;
// p[u] = find(p[u]);
// if(p[u] != u) g[p[u]].insert(mp(pf[u],u));
// if(p[u] != u && sm[u] >= 0 && -min(pf[p[u]],sm[p[u]]+pf[u]) <= sm[find(0)]) {
// q.push(mp(p[u],u));
// }
// if(-pf[u] <= sm[find(0)]) {
// while(g[u].size() && -(sm[u]+g[u].begin()->fr) <= sm[find(0)]) {
// int vv = find(g[u].begin()->sc);
// g[u].erase(g[u].begin());
// if(find(u) != find(vv) && sm[vv] >= 0 && -min(pf[u],sm[u]+pf[vv]) <= sm[find(0)]) {
// q.push(mp(u,vv));
// }
// }
// }
// }
// }
// for(int it = 1; it <= n; it++) {
// for(int i = 1; i <= n; i++) {
// int u = find(p[i]);
// int v = find(i);
// if(u == v) continue;
// if(sm[v] >= 0 && -min(pf[u],sm[u]+pf[v]) <= sm[find(0)]) {
// // cout << it << " " << i << " " << p[i] << " " << -min(pf[u],sm[u]+pf[v]) << endl;
// int newsm = sm[u]+sm[v];
// int newpf = min(pf[u],sm[u]+pf[v]);
// join(u,v);
// v = find(v);
// sm[v] = newsm;
// pf[v] = newpf;
// }
// }
// }
cout << sm[find(0)]-s << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# | 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... |