This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#include "dreaming.h"
#endif
const int N = 100'000 + 10;
vector<pair<int, int>> ad[N];
bool mk[N];
int dpin[N], dpout[N];
void dfs1(int u, int p = 0) {
mk[u] = true;
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
dfs1(v, u);
dpin[u] = max(dpin[u], dpin[v] + w);
}
}
int dfs2(int u, int p = 0) {
array<pair<int, int>, 2> best;
best[0] = best[1] = {-1e9, -1};
auto add = [&](pair<int, int> x) {
if (best[1] < x) swap(best[1], x);
if (best[0] < best[1]) swap(best[0], best[1]);
};
add({dpout[u], u});
for (const auto& [v, w] : ad[u]) if (v != p) add({dpin[v] + w, v});
int ret = u;
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
dpout[v] = (best[0].second == v ? best[1].first : best[0].first) + w;
int nv = dfs2(v, u);
if (max(dpin[nv], dpout[nv]) < max(dpin[ret], dpout[ret])) ret = nv;
}
return ret;
}
int travelTime(int n, int m, int l, int A[],int B[],int T[]) {
for (int i = 1; i <= m; ++i) {
int u = A[i], v = B[i], w = T[i];
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
vector<int> ed;
for (int i = 0; i < n; ++i) {
if (mk[i]) continue;
dfs1(i);
int x = dfs2(i);
ed.push_back(x);
}
sort(ed.begin(), ed.end(), [&](const auto& a, const auto & b) {
return max(dpin[a], dpout[a]) > max(dpin[b], dpout[b]);
});
for (const auto& x : ed) {
if (x == ed[0]) continue;
ad[ed[0]].push_back({x, l});
ad[x].push_back({ed[0], l});
}
memset(dpin, 0, sizeof dpin);
memset(dpout, 0, sizeof dpout);
dfs1(1); dfs2(1);
int answer = 0;
for (int i = 0; i < n; ++i) answer = max({answer, dpin[i], dpout[i]});
return answer;
}
#ifdef LOCAL
int n, m, l;
int A[N], B[N], T[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> l;
for (int i = 1; i <= m; ++i) cin >> A[i] >> B[i] >> T[i];
cout << travelTime(n, m, l, A, B, T) << "\n";
}
#endif
# | 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... |