#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 int long long
#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
islands.cpp:57:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | main() {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
17 ms |
24044 KB |
Output is correct |
4 |
Correct |
19 ms |
23916 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
17 ms |
23916 KB |
Output is correct |
7 |
Correct |
20 ms |
23916 KB |
Output is correct |
8 |
Correct |
17 ms |
23916 KB |
Output is correct |
9 |
Correct |
19 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23916 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
24188 KB |
Output is correct |
2 |
Correct |
18 ms |
24172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
24172 KB |
Output is correct |
2 |
Correct |
19 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
25324 KB |
Output is correct |
2 |
Correct |
36 ms |
27116 KB |
Output is correct |
3 |
Correct |
29 ms |
25836 KB |
Output is correct |
4 |
Correct |
23 ms |
24940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
28624 KB |
Output is correct |
2 |
Correct |
58 ms |
33004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
42220 KB |
Output is correct |
2 |
Correct |
113 ms |
43756 KB |
Output is correct |
3 |
Correct |
139 ms |
50284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
59244 KB |
Output is correct |
2 |
Correct |
226 ms |
74476 KB |
Output is correct |
3 |
Correct |
244 ms |
76140 KB |
Output is correct |
4 |
Correct |
298 ms |
91504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
120384 KB |
Output is correct |
2 |
Correct |
978 ms |
126568 KB |
Output is correct |
3 |
Correct |
334 ms |
95084 KB |
Output is correct |
4 |
Correct |
435 ms |
119416 KB |
Output is correct |
5 |
Correct |
493 ms |
121964 KB |
Output is correct |
6 |
Correct |
1429 ms |
112760 KB |
Output is correct |
7 |
Correct |
456 ms |
131072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
441 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |