#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
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 new_root = root;
y_combinator([&](auto dfs, int v, int p) -> void {
was[v] = 1;
if (dist[new_root] < dist[v]) {
new_root = 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);
int t = (int) 2e9;
dist[new_root] = 0;
int s = y_combinator([&](auto dfs, int v, int p) -> int {
int res = dist[v];
for (int id : g[v]) {
int to = v ^ a[id] ^ b[id];
if (to == p) {
continue;
}
dist[to] = dist[v] + w[id];
res = max(res, dfs(to, v));
}
if (t > max(dist[v], res - dist[v])) {
t = max(dist[v], res - dist[v]);
}
return res;
})(new_root, -1);
ans = max(ans, s);
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
Compilation message
/usr/bin/ld: /tmp/ccI1IYVQ.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status