#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
using namespace std;
using LL = long long;
#define watch(x) cerr << (#x) << " is " << x << endl
vector<bool> vis, cycle;
vector<array<int, 2>> nxt;
vector<LL> dl;
vector<LL> f;
LL max_path(const vector<int>& c, const vector<vector<array<LL, 3>>>& adj) {
auto find_cycle = [&](auto&& self, int v, int pid = -1) -> bool {
if (vis[v]) return cycle[v] = true;
vis[v] = true;
for (auto [u, w, eid] : adj[v]) {
if (eid == pid) continue;
bool works = self(self, u, eid);
if (works) {
nxt[v] = {u, w};
return cycle[v] ? false : cycle[v] = true;
}
}
return false;
};
find_cycle(find_cycle, c[0]);
auto path_in_tree = [&](auto&& self, int v, int p = -1) -> LL {
LL ans = 0;
for (auto [u, w, eid] : adj[v]) {
if (u == p || cycle[u]) continue;
ans = max(ans, w + self(self, u, v));
}
return ans;
};
int start = -1;
for (int i : c) {
if (cycle[i]) {
start = i;
f[i] = path_in_tree(path_in_tree, i);
}
}
multiset<LL> L;
LL len = 0;
{
int v = start;
dl[v] = 0;
do {
auto [nv, lw] = nxt[v];
len += lw;
if (v != start) L.insert(f[v] + dl[v]);
if (nv != start) dl[nv] = dl[v] + lw;
v = nv;
} while (v != start);
}
assert(dl[start] == 0);
LL bias = 0;
LL ans = 0;
{
int v = start;
do {
// watch(v);
// watch(bias);
// cerr << "L: "; for (LL i : L) cerr << i << " "; cerr << endl;
// cerr << endl;
ans = max(ans, *prev(L.end()) - bias + f[v]);
int nv = nxt[v][0];
if (nv == start) break;
bias += nxt[v][1];
L.insert(f[v] + dl[v] + len);
if (L.count(f[nv] + dl[nv])) L.erase(L.find(f[nv] + dl[nv]));
v = nv;
} while (v != start);
}
return ans;
}
int main() {
#ifdef LORENZO
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vector<vector<array<LL, 3>>> adj(n);
for (int i = 0; i < n; ++i) {
int a; cin >> a; --a;
int w; cin >> w;
adj[i].push_back({a, w, i});
adj[a].push_back({i, w, i});
}
vis.assign(n, false);
vector<vector<int>> cc;
auto dfs = [&](auto&& self, int v) -> void {
vis[v] = true;
cc.back().push_back(v);
for (auto [u, _, __] : adj[v]) {
if (!vis[u]) {
self(self, u);
}
}
};
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
cc.push_back(vector<int>(0));
dfs(dfs, i);
}
}
f.assign(n, 0);
dl.assign(n, 1e18);
nxt.assign(n, {-1,-1});
vis.assign(n, false);
cycle.assign(n, false);
LL ans = 0;
for (const auto& c : cc) {
ans += max_path(c, adj);
}
cout << ans << "\n";
}
Compilation message
islands.cpp: In instantiation of 'max_path(const std::vector<int>&, const std::vector<std::vector<std::array<long long int, 3> > >&)::<lambda(auto:23&&, int, int)> [with auto:23 = max_path(const std::vector<int>&, const std::vector<std::vector<std::array<long long int, 3> > >&)::<lambda(auto:23&&, int, int)>&]':
islands.cpp:27:31: required from here
islands.cpp:21:16: warning: narrowing conversion of 'u' from 'std::tuple_element<0, std::array<long long int, 3> >::type' {aka 'long long int'} to 'int' [-Wnarrowing]
21 | nxt[v] = {u, w};
islands.cpp:21:16: warning: narrowing conversion of 'w' from 'std::tuple_element<1, std::array<long long int, 3> >::type' {aka 'long long int'} to 'int' [-Wnarrowing]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Incorrect |
1 ms |
280 KB |
Output isn't correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
452 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
584 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2516 KB |
Output is correct |
2 |
Correct |
24 ms |
6472 KB |
Output is correct |
3 |
Correct |
11 ms |
3244 KB |
Output is correct |
4 |
Incorrect |
6 ms |
1748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
8500 KB |
Output is correct |
2 |
Correct |
74 ms |
14316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
25844 KB |
Output is correct |
2 |
Correct |
150 ms |
35040 KB |
Output is correct |
3 |
Correct |
195 ms |
49976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
49224 KB |
Output is correct |
2 |
Correct |
265 ms |
84544 KB |
Output is correct |
3 |
Correct |
450 ms |
100304 KB |
Output is correct |
4 |
Correct |
469 ms |
128720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
389 ms |
122764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
336 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |