/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
typedef long long i64;
const int MAX_N = 1e6 + 5;
vector<vector<int>> g(MAX_N);
int Time = 0, cycle_start, cycle_end;
vector<int> nxt(MAX_N), cost(MAX_N), par(MAX_N), cycle;
bitset<MAX_N> stack_status;
bitset<MAX_N> is_done;
bitset<MAX_N> is_cycle;
void find_cycle(int node, int p) {
stack_status[node] = 1;
for (auto et : g[node]) {
if (et == p && !(nxt[p] == node && nxt[node] == p)) continue;
if (stack_status[et] == 0) {
par[et] = node;
find_cycle(et, node);
} else if (!is_done[et] && stack_status[et] == 1) {
cycle_start = et;
cycle_end = node;
}
}
is_done[node] = 1;
}
int root;
pair<i64, int> dfs(int node, int p) {
pair<i64, int> best_way = {0, node};
for (auto et : g[node]) {
const int w = (nxt[node] == et ? cost[node] : cost[et]);
if (et != root && is_cycle[et]) continue;
if (et != p) {
pair<i64, int> cur = dfs(et, node);
cur.first += w;
best_way = max(best_way, cur);
}
}
return best_way;
}
i64 get_depth(int node, int p) {
i64 mx = 0;
for (auto et : g[node]) {
const int w = (nxt[node] == et ? cost[node] : cost[et]);
if (is_cycle[et]) continue;
if (et != p) {
mx = max(mx, w + get_depth(et, node));
}
}
return mx;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int st = 0; st < n; st++) {
int et, w;
cin >> et >> w;
--et;
nxt[st] = et;
cost[st] = w;
g[st].emplace_back(et);
g[et].emplace_back(st);
}
i64 ans = 0;
for (int i = 0; i < n; i++) {
if (stack_status[i] != 0) continue;
cycle.clear();
find_cycle(i, -1);
i64 cycle_cost = 0;
for (int cur = cycle_end; cur != cycle_start; cur = par[cur]) {
cycle.emplace_back(cur);
is_cycle[cur] = 1;
}
is_cycle[cycle_start] = 1;
cycle.emplace_back(cycle_start);
reverse(cycle.begin(), cycle.end());
for (int j = 1; j < (int) cycle.size(); j++) {
const int st = cycle[j - 1];
const int et = cycle[j];
const int w = (nxt[st] == et ? cost[st] : cost[et]);
cycle_cost += w;
}
cycle_cost += (nxt[cycle.back()] == cycle[0] ? cost[cycle.back()] : cost[0]);
debug(cycle_cost);
i64 res = 0;
for (auto to : cycle) {
root = to;
pair<i64, int> best_way = dfs(root, -1);
res = max(res, dfs(best_way.second, -1).first);
}
i64 ccw = get_depth(cycle[0], -1);
i64 cw = get_depth(cycle[0], -1);
i64 start = 0;
for (int j = 1; j < (int) cycle.size(); j++) {
const int st = cycle[j - 1];
const int et = cycle[j];
const int w = (nxt[st] == et ? cost[st] : cost[et]);
start += w;
res = max(res, cw + start + get_depth(et, -1));
cw = max(cw, -start + get_depth(et, -1));
const i64 other = cycle_cost - start;
res = max(res, ccw + other + get_depth(et, -1));
ccw = max(ccw, start + get_depth(et, -1));
}
res = max({res, cw, ccw});
ans += res;
}
cout << ans << '\n';
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:22:20: warning: statement has no effect [-Wunused-value]
22 | #define debug(...) 47
| ^~
islands.cpp:116:5: note: in expansion of macro 'debug'
116 | debug(cycle_cost);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
35584 KB |
Output is correct |
2 |
Correct |
24 ms |
35584 KB |
Output is correct |
3 |
Correct |
25 ms |
35580 KB |
Output is correct |
4 |
Correct |
24 ms |
35716 KB |
Output is correct |
5 |
Correct |
26 ms |
35584 KB |
Output is correct |
6 |
Correct |
23 ms |
35648 KB |
Output is correct |
7 |
Correct |
24 ms |
35584 KB |
Output is correct |
8 |
Incorrect |
23 ms |
35584 KB |
Output isn't correct |
9 |
Correct |
24 ms |
35704 KB |
Output is correct |
10 |
Correct |
24 ms |
35712 KB |
Output is correct |
11 |
Correct |
24 ms |
35576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
35712 KB |
Output is correct |
2 |
Correct |
24 ms |
35584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
35704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
36344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
39288 KB |
Output is correct |
2 |
Correct |
72 ms |
40700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
44152 KB |
Output is correct |
2 |
Correct |
124 ms |
50420 KB |
Output is correct |
3 |
Correct |
146 ms |
58740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
50016 KB |
Output is correct |
2 |
Correct |
237 ms |
71368 KB |
Output is correct |
3 |
Correct |
264 ms |
80236 KB |
Output is correct |
4 |
Correct |
323 ms |
94456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
71268 KB |
Output is correct |
2 |
Correct |
1053 ms |
117100 KB |
Output is correct |
3 |
Correct |
346 ms |
62712 KB |
Output is correct |
4 |
Correct |
458 ms |
97772 KB |
Output is correct |
5 |
Correct |
462 ms |
97776 KB |
Output is correct |
6 |
Correct |
1634 ms |
66832 KB |
Output is correct |
7 |
Correct |
474 ms |
126952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
445 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |