이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
priority_queue<pair<int,int>> pq;
int find(int u) {
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);
dsz[u]+= dsz[v];
ds[v] = u;
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;
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;
}
}
}
int st = find(0);
cout << sm[st]-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... |