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;
using ll = long long;
using ari2 = array<int, 2>;
using arl2 = array<ll, 2>;
#define cerr if (false) cout
constexpr int mxN = 1e6;
int N, F[mxN][2];
vector<ari2> adj[mxN];
bool seen[mxN], in_cyc[mxN];
ll res;
ll dfs(int f, ll d) {
ll ret = d, ret2 = d;
seen[f] = true;
int cnt = 0;
for (auto [nf, v] : adj[f]) {
if (in_cyc[nf]) continue;
ll bruh = dfs(nf, d+v);
cnt++;
ret2 = max(ret2, bruh);
if (ret2 > ret) swap(ret2, ret);
}
res = max({res, cnt > 1 ? ret2 + ret - 2ll * d : 0ll, ret});
return ret;
};
signed main() {
#ifdef cerr
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("isl.in", "r", stdin);
//freopen("isl.out", "w", stdout);
#endif
cin >> N;
for (int i = 0; i < N; i++) {
cin >> F[i][0] >> F[i][1];
adj[--F[i][0]].push_back({i, F[i][1]});
}
ll ans = 0;
for (int i = 0; i < N; i++) {
if (seen[i]) continue;
int cys = i;
do {
seen[cys] = true;
cys = F[cys][0];
} while (!seen[cys]);
int cur = cys;
do {
in_cyc[cur] = true;
cur = F[cur][0];
} while (cur != cys);
res = 0;
vector<arl2> P;
ll x = 0;
for (bool _ : {0, 1}) {
int ind = 0;
do {
P.push_back({x, _ ? P[ind++][1] : dfs(cur, 0ll)});
x += F[cur][1];
cur = F[cur][0];
} while (cur != cys);
}
deque<pair<ll, int>> dq;
for (int j = 0; j < P.size(); j++) {
auto [p, v] = P[j];
while (dq.size() && dq.front().second <= j-P.size()/2) {
dq.pop_front();
}
res = max(res, (dq.size() ? dq.front().first + p : 0ll) + v);
while (dq.size() && dq.back().first <= v-p) {
dq.pop_back();
}
dq.push_back({v-p, j});
}
dq.clear();
for (int j = P.size()-1; ~j; j--) {
auto [p, v] = P[j];
while (dq.size() && dq.front().second >= j+P.size()/2) {
dq.pop_front();
}
res = max(res, (dq.size() ? dq.front().first - p : 0ll) + v);
while (dq.size() && dq.back().first <= v+p) {
dq.pop_back();
}
dq.push_back({v+p, j});
}
ans += res;
}
cout << ans << '\n';
}
Compilation message (stderr)
islands.cpp: In function 'int main()':
islands.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j = 0; j < P.size(); j++) {
| ~~^~~~~~~~~~
islands.cpp:74:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while (dq.size() && dq.front().second <= j-P.size()/2) {
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
islands.cpp:87:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while (dq.size() && dq.front().second >= j+P.size()/2) {
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# | 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... |