#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pil = pair<int, lint>;
using pli = pair<lint, int>;
const int MAX_N = 1e6 + 5;
int n;
vector<pil> adj[MAX_N];
bool seen[MAX_N];
vector<int> cyc;
bool in_cyc[MAX_N];
vector<int> ord;
int par1[MAX_N];
void dfs1(int u) {
seen[u] = true, ord.push_back(u);
bool par_seen = false;
for (pil x : adj[u]) {
int v = x.first;
if (v == par1[u] && !par_seen) { par_seen = true; continue; }
if (!seen[v]) { par1[v] = u, dfs1(v); continue; }
if (cyc.size()) continue;
int i = ord.size() - 1;
while (true) {
cyc.push_back(ord[i]), in_cyc[ord[i]] = true;
if (ord[i] == v) break;
i--;
}
}
ord.pop_back();
}
lint dp[MAX_N];
int par2[MAX_N];
lint dfs2(int u) {
lint ans = 0;
multiset<lint> trans;
for (pil x : adj[u]) {
int v = x.first;
if (v == par2[u] || in_cyc[v]) continue;
par2[v] = u, ans = max(ans, dfs2(v));
dp[u] = max(dp[u], dp[v] + x.second);
trans.insert(dp[v] + x.second);
if (trans.size() > 2) trans.erase(trans.begin());
}
lint new_ans = 0;
for (int i = 1; i <= 2; i++)
if (trans.size()) { new_ans += *trans.rbegin(); trans.erase(next(trans.end(), -1)); }
ans = max(ans, new_ans);
return ans;
}
vector<lint> lens;
lint len_sum;
void precomp() {
if (cyc.size() == 2) {
set<int> lens_set;
for (int u : cyc) {
for (pil x : adj[u]) {
int v = x.first;
if (!in_cyc[v]) continue;
lens_set.insert(x.second);
}
}
lens.insert(lens.begin(), lens_set.begin(), lens_set.end());
len_sum = lens[0] + lens[1];
return;
}
for (int i = 0; i < cyc.size(); i++) {
int aft = (i + 1 == (int) cyc.size()) ? 0 : i + 1;
int u = cyc[i], v = cyc[aft];
for (pil x : adj[u])
if (x.first == v) lens.push_back(x.second), len_sum += x.second;
}
}
lint comp() {
multiset<lint> trans;
vector<lint> val = {0};
for (int i = 1; i < cyc.size(); i++)
val.push_back(val.back() + lens[i - 1]);
for (int i = 1; i < cyc.size(); i++)
val[i] = dp[cyc[i]] - val[i], trans.insert(val[i]);
lint shift = 0;
lint ans = 0;
for (int i = 0; i < cyc.size(); i++) {
ans = max(ans, dp[cyc[i]] + len_sum + *trans.rbegin() + shift);
if (i + 1 == (int) cyc.size()) continue;
trans.erase(trans.find(val[i + 1]));
val[i] = dp[cyc[i]] - len_sum - shift;
trans.insert(val[i]);
shift += lens[i];
}
return ans;
}
int main() {
// freopen("isl.in", "r", stdin), freopen("isl.out", "w", stdout);
cin >> n;
for (int u = 1; u <= n; u++) {
int v; cin >> v;
lint len; cin >> len;
adj[u].push_back({v, len}), adj[v].push_back({u, len});
}
lint ans = 0;
for (int u = 1; u <= n; u++) {
if (seen[u]) continue;
cyc.clear(), lens.clear(), len_sum = 0;
lint new_ans = 0;
dfs1(u);
for (int u : cyc) new_ans = max(new_ans, dfs2(u));
precomp();
new_ans = max(new_ans, comp());
reverse(cyc.begin(), cyc.end()), reverse(lens.begin(), next(lens.end(), -1));
new_ans = max(new_ans, comp());
ans += new_ans;
}
cout << ans << '\n';
}
Compilation message
islands.cpp: In function 'void precomp()':
islands.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < cyc.size(); i++) {
| ~~^~~~~~~~~~~~
islands.cpp: In function 'lint comp()':
islands.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 1; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
islands.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int i = 1; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
islands.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < cyc.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
26968 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
5 ms |
26980 KB |
Output is correct |
7 |
Correct |
4 ms |
26972 KB |
Output is correct |
8 |
Correct |
6 ms |
26996 KB |
Output is correct |
9 |
Correct |
4 ms |
26972 KB |
Output is correct |
10 |
Correct |
5 ms |
26972 KB |
Output is correct |
11 |
Correct |
5 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27128 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
8 ms |
27480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28464 KB |
Output is correct |
2 |
Correct |
33 ms |
32084 KB |
Output is correct |
3 |
Correct |
19 ms |
28760 KB |
Output is correct |
4 |
Correct |
11 ms |
27736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
34028 KB |
Output is correct |
2 |
Correct |
76 ms |
38980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
49044 KB |
Output is correct |
2 |
Correct |
196 ms |
55444 KB |
Output is correct |
3 |
Correct |
266 ms |
73292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
61936 KB |
Output is correct |
2 |
Correct |
380 ms |
97096 KB |
Output is correct |
3 |
Correct |
632 ms |
103148 KB |
Output is correct |
4 |
Correct |
751 ms |
131072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
504 ms |
96152 KB |
Output is correct |
2 |
Runtime error |
712 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
516 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |