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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll ans, ed, total, wtpref, fs, snd, ldpath, lpath;
int N, st, rn, tort, hare, link[1000005];
ii nxt[1000005];
bitset<1000005> oncyc;
vector<ii> adj[1000005];
int find(int x) {
if (x == link[x]) return x;
return link[x] = find(link[x]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (rng() & 1ll) swap(a, b);
link[b] = a;
}
void find_diam(int n, int e = -1, ll d = 0) {
if (d > ed) {
ed = d;
st = n;
}
for (auto u : adj[n])
if ((!oncyc[u.first] || u.first == rn) && u.first != e)
find_diam(u.first, n, d + (ll)u.second);
}
main() {
oncyc.reset();
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) link[i] = i;
for (int i = 1; i <= N; i++) {
cin >> rn >> st;
nxt[i] = mp(rn, st);
adj[i].eb(rn, st);
adj[rn].eb(i, st);
unite(i, rn);
}
for (int i = 1; i <= N; i++) {
if (i == find(i)) {
lpath = 0;
tort = hare = i;
do {
tort = nxt[tort].first;
hare = nxt[nxt[hare].first].first;
} while (tort != hare);
total = wtpref = 0;
fs = snd = -1;
do {
total += nxt[tort].second;
oncyc[tort] = 1;
tort = nxt[tort].first;
} while (tort != hare);
do {
rn = tort;
ed = st = -1;
find_diam(tort);
ldpath = ed;
ed = -1;
find_diam(st);
lpath = max(lpath, ed);
if (fs != -1) lpath = max({lpath, fs + ldpath + wtpref, snd + ldpath - wtpref + total});
fs = max(fs, ldpath - wtpref);
snd = max(snd, ldpath + wtpref);
wtpref += nxt[tort].second;
tort = nxt[tort].first;
} while (tort != hare);
ans += lpath;
}
}
cout << ans << '\n';
}
Compilation message (stderr)
islands.cpp:56:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
56 | main() {
| ^
# | 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... |