이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* 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
const int MAX_N = 4e3 + 5;
int Time = 0;
vector<vector<pair<int, int>>> g(MAX_N);
vector<int> low(MAX_N), disc(MAX_N);
map<int, int> cnt[MAX_N];
set<pair<int, int>> all_edge;
set<pair<int, int>> bridge;
void is_valid_bridge(int st, int et) {
if (max(cnt[st][et], cnt[et][st]) <= 1) {
bridge.insert({st, et});
}
}
void find_bridge(int node, int p) {
low[node] = disc[node] = ++Time;
for (auto to : g[node]) {
const int et = to.first;
all_edge.insert({node, et});
if (et == p) continue;
if (!disc[et]) {
find_bridge(et, node);
low[node] = min(low[node], low[et]);
if (disc[node] < low[et]) {
// cout << node << ' ' << et << '\n';
is_valid_bridge(node, et);
}
} else {
low[node] = min(low[node], disc[et]);
}
}
}
int blocked_st, blocked_et;
pair<long long, int> dist;
void dfs(int node, int p, long long w) {
dist = max(dist, {w, node});
for (auto to : g[node]) {
const int et = to.first;
const int cost = to.second;
if (et == p) continue;
if (cnt[node][et] == 1) {
if (blocked_st == node && blocked_et == et) continue;
if (blocked_et == node && blocked_st == et) continue;
}
dfs(et, node, w + cost);
}
}
long long find_diameter(int node) {
dist = {-1, -1};
dfs(node, -1, 0);
node = dist.second;
dfs(node, -1, 0);
return dist.first;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n;
cin >> n;
for (int st = 0; st < n; st++) {
int et, w;
cin >> et >> w;
--et;
++cnt[st][et];
++cnt[et][st];
g[st].emplace_back(et, w);
g[et].emplace_back(st, w);
}
long long best_way = 0;
for (int i = 0; i < n; i++) {
if (!disc[i]) {
all_edge = set<pair<int, int>>();
bridge = set<pair<int, int>>();
find_bridge(i, -1);
long long res = 0;
for (auto to : all_edge) {
if (bridge.find({to.first, to.second}) != bridge.end() || bridge.find({to.second, to.first}) != bridge.end()) continue;
blocked_st = to.first;
blocked_et = to.second;
res = max(res, find_diameter(i));
}
best_way += res;
}
}
cout << best_way << '\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... |