This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* 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<tuple<int, int, int>>> g(MAX_N);
int Time = 0;
stack<int> stk;
vector<int> low(MAX_N), disc(MAX_N);
vector<int> cycle;
set<int> in_cycle;
map<int, int> edge_cost[MAX_N];
void find_cycle(int node, int p) {
//~ debug(node);
low[node] = disc[node] = ++Time;
stk.push(node);
for (auto to : g[node]) {
const int et = get<0>(to);
//~ const int w = get<1>(to);
const int delta = get<2>(to);
if (delta == p) continue;
if (!disc[et]) {
find_cycle(et, delta);
low[node] = min(low[node], low[et]);
} else {
low[node] = min(low[node], disc[et]);
}
}
if (low[node] == disc[node]) {
//~ cout << "S: " << enode << '\n';
vector<int> cur_cycle;
while(true) {
const int cur = stk.top();
stk.pop();
cur_cycle.emplace_back(cur);
if (cur == node) break;
}
if(cur_cycle.size() > 1) {
for (auto to : cur_cycle) in_cycle.insert(to);
cycle = cur_cycle;
}
}
}
bool is_in_cycle(int node) {
return in_cycle.find(node) != in_cycle.end();
}
int root;
pair<i64, int> dfs(int node, int p) {
pair<i64, int> best_way = {0, node};
for (auto to : g[node]) {
const int et = get<0>(to);
const int w = get<1>(to);
if (et != root && is_in_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_diameter(int node) {
pair<i64, int> cur = dfs(node, -1);
return dfs(cur.second, -1).first;
}
vector<i64> h(MAX_N);
void get_depth(int node, int p, i64 H) {
h[root] = max(h[root], H);
for (auto to : g[node]) {
const int et = get<0>(to);
const int w = get<1>(to);
if (is_in_cycle(et)) continue;
if (et != p) {
get_depth(et, node, H + w);
}
}
}
vector<bool> vis(MAX_N);
i64 find_cost(int node, int p) {
vis[node] = true;
i64 res = 0;
for (auto to : g[node]) {
const int et = get<0>(to);
const int w = get<1>(to);
if (is_in_cycle(node) && is_in_cycle(et)) res += w;
if (!vis[et]) {
res += find_cost(et, node);
}
}
return res;
}
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;
edge_cost[st][et] = w;
g[st].emplace_back(make_tuple(et, w, st));
g[et].emplace_back(make_tuple(st, w, st));
}
i64 ans = 0;
for (int i = 0; i < n; i++) {
if (disc[i]) continue;
in_cycle = set<int>();
cycle.clear();
find_cycle(i, -1);
i64 res = 0;
for (auto to : cycle) {
root = to;
res = max(res, get_diameter(root));
get_depth(root, -1, 0);
}
assert(!cycle.empty());
root = i;
i64 cycle_cost = find_cost(i, -1) / 2;
i64 clockwise = h[cycle[0]];
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 = (edge_cost[st][et] == 0 ? edge_cost[et][st] : edge_cost[st][et]);
start += w;
res = max(res, clockwise + start + h[et]);
clockwise = max(clockwise, -start + h[et]);
}
res = max(res, clockwise);
clockwise = h[cycle[0]];
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 = (edge_cost[st][et] == 0 ? edge_cost[et][st] : edge_cost[st][et]);
start += w;
const i64 other = cycle_cost - start;
res = max(res, clockwise + other + h[et]);
clockwise = max(clockwise, start + h[et]);
}
res = max(res, clockwise);
ans += res;
}
cout << ans << '\n';
return 0;
}
# | 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... |