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;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
#include "dreaming.h"
template <class F>
struct y_combinator_result {
F f;
template <class T>
explicit y_combinator_result(T &&f_) : f(std::forward<T>(f_)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return f(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class F>
decltype(auto) y_combinator(F &&f) {
return y_combinator_result<std::decay_t<F>>(std::forward<F>(f));
}
int travelTime(int n, int m, int l, int a[], int b[], int w[]) {
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
g[a[i]].emplace_back(i);
g[b[i]].emplace_back(i);
}
int ans = 0;
vector<int> was(n);
vector<int> dist(n);
vector<int> c;
for (int root = 0; root < n; root++) {
if (was[root]) {
continue;
}
int r1 = root;
y_combinator([&](auto dfs, int v, int p) -> void {
was[v] = 1;
if (dist[r1] < dist[v]) {
r1 = v;
}
for (int id : g[v]) {
int to = v ^ a[id] ^ b[id];
if (to == p) {
continue;
}
dist[to] = dist[v] + w[id];
dfs(to, v);
}
})(root, -1);
dist[r1] = 0;
int r2 = r1;
y_combinator([&](auto dfs, int v, int p) -> void {
was[v] = 1;
if (dist[r2] < dist[v]) {
r2 = v;
}
for (int id : g[v]) {
int to = v ^ a[id] ^ b[id];
if (to == p) {
continue;
}
dist[to] = dist[v] + w[id];
dfs(to, v);
}
})(r1, -1);
ans = max(ans, dist[r2]);
int t = dist[r2];
dist[r2] = 0;
y_combinator([&](auto dfs, int v, int p) -> void {
for (int id : g[v]) {
int to = v ^ a[id] ^ b[id];
if (to == p) {
continue;
}
t = min(t, max(dist[to], dist[v] + w[id]));
dist[to] = dist[v] + w[id];
dfs(to, v);
}
})(r2, -1);
c.emplace_back(t);
}
sort(c.rbegin(), c.rend());
debug(c);
if (c.size() > 1) {
ans = max(ans, c[0] + c[1] + l);
}
if (c.size() > 2) {
ans = max(ans, c[1] + c[2] + l * 2);
}
return ans;
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a[] = {0, 8, 2, 5, 5, 1, 1, 10};
int b[] = {8, 2, 7, 11, 1, 3, 9, 6};
int t[] = {4, 2, 4, 3, 7, 1, 5, 3};
cout << travelTime(12, 8, 2, a, b, t) << '\n';
return 0;
}
#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... |