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;
// #define int long long
#define ll long long
#define pii pair<ll, int>
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define NYOOM ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
const int INF = 1e9 + 7;
const ll LLINF = 1ll<<60;
const int maxn = 1e6 + 10;
int n, p[maxn], l[maxn], idx[maxn] = {0}; // 0 = unseen, 1 = seeing, 2 = seen
ll val[maxn], cur = 0, re = 0;
// bool on_cycle[maxn] = {0};
bitset<maxn> on_cycle, seen, cur_seen;
vector<int> adj[maxn];
stack<pii> s;
// void dfs(int v, int p){
// val[v] = 0;
// ll best = -1, second_best = -1;
// for (auto u : adj[v]){
// if (on_cycle[u] || u == p) continue;
// dfs(u, v);
// val[v] = max(val[v], val[u] + l[u]);
// if (best == -1) best = val[u] + l[u];
// else second_best = max(second_best, val[u] + l[u]);
// if (second_best > best) swap(best, second_best);
// }
// cur = max(cur, best + second_best);
// }
void dfs(int v, int p){
s.push({v, p});
while (!s.empty()){
int v = s.top().F, p = s.top().S;
bool seen_all_children = true;
while (idx[v] < adj[v].size()){
int u = adj[v][idx[v]++];
if (on_cycle[u] || u == p) continue;
seen_all_children = false;
s.push({u, v}); break;
}
if (!seen_all_children) continue;
val[v] = 0;
ll best = -1, second_best = -1;
for (auto u : adj[v]){
if (on_cycle[u] || u == p) continue;
val[v] = max(val[v], val[u] + l[u]);
if (best == -1) best = val[u] + l[u];
else second_best = max(second_best, val[u] + l[u]);
if (second_best > best) swap(best, second_best);
}
cur = max(cur, best + second_best);
s.pop();
}
}
void solve(int s, int sz, ll total_sum){
int v = s;
ll cw = -LLINF, ccw = -LLINF, cur_sum = 0;
FOR(i, sz){
cur = max(cur, cw + val[v] + cur_sum);
cur = max(cur, ccw + val[v] + (total_sum - cur_sum));
cw = max(cw, val[v] - cur_sum);
ccw = max(ccw, val[v] + cur_sum);
cur_sum += l[v]; v = p[v];
}
}
void find_cycle(int s){
int v = s;
while (!cur_seen[v]){ // while unseen
cur_seen[v] = true; v = p[v];
}
if (cur_seen[v] && !seen[v]){ // found a cycle
cur = 0; int sz = 0; ll total_sum = 0;
while (!on_cycle[v]){
total_sum += l[v];
sz++; on_cycle[v] = true; v = p[v];
}
FOR(i, sz){
dfs(v, v); v = p[v];
}
solve(v, sz, total_sum);
re += cur;
}
v = s;
while (!seen[v]){
seen[v] = true; v = p[v];
}
}
signed main(){
NYOOM;
cin >> n;
for (int i = 1; i <= n; i++){
cin >> p[i] >> l[i];
adj[p[i]].PB(i);
}
for (int i = 1; i <= n; i++){
if (seen[i] != 2) find_cycle(i);
}
cout << re;
}
Compilation message (stderr)
islands.cpp: In function 'void dfs(int, int)':
islands.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | while (idx[v] < adj[v].size()){
| ~~~~~~~^~~~~~~~~~~~~~~
# | 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... |
# | 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... |