#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
const int MaxN = 1e5 + 5;
int travelTime(int N, int M, int L, int A[], int B[], int T[]);
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }
const int Inf = 0x3f3f3f3f;
int n, m, l;
vector<int> pref[MaxN], suff[MaxN];
vector<pair<int, int>> adj[MaxN];
int vis[MaxN];
int dep[MaxN], dp[MaxN];
void dfs(int u, int p) {
vis[u] = true;
for (auto [v, w]: adj[u]) if (v != p) {
dep[v] = dep[u] + w;
dfs(v, u);
maximize(dp[u], dp[v] + w);
}
//cout << "? " << u << " " << dp[u] << endl;
}
int ans;
void reroot(int u, int p) {
//cout << u << " " << dp[u] << endl;
minimize(ans, dp[u]);
for (int i = 0; i < sz(adj[u]); ++i) {
auto [v, w] = adj[u][i];
pref[u].emplace_back(dp[v] + w);
suff[u].emplace_back(dp[v] + w);
}
for (int i = 0; i < sz(adj[u]); ++i) {
auto [v, w] = adj[u][i];
if (i) maximize(pref[u][i], pref[u][i - 1]);
}
for (int i = sz(adj[u]) - 1; i >= 0; --i) {
auto [v, w] = adj[u][i];
if (i + 1 < sz(adj[u])) maximize(suff[u][i], suff[u][i + 1]);
}
for (int i = 0; i < sz(adj[u]); ++i) {
auto [v, w] = adj[u][i];
if (v == p) continue;
int old = dp[v];
int nw = max((i ? pref[u][i - 1] : 0), (i + 1 < sz(adj[u]) ? suff[u][i + 1] : 0));
dp[u] = nw;
dp[v] = max(dp[u] + w, old);
reroot(v, u);
dp[u] = pref[u].back();
dp[v] = old;
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
m = M;
l = L;
for (int i = 0; i < m; ++i) {
int u = A[i];
int v = B[i];
int w = T[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
vector<int> best;
for (int i = 0; i < n; ++i) if (!vis[i]) {
dfs(i, i);
ans = Inf;
reroot(i, i);
best.push_back(ans);
}
sort(best.begin(), best.end(), greater<int>());
return best[0] + best[1] + l;
}
Compilation message
dreaming.cpp: In function 'void reroot(int, int)':
dreaming.cpp:46:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
46 | auto [v, w] = adj[u][i];
| ^~~~~~
dreaming.cpp:51:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
51 | auto [v, w] = adj[u][i];
| ^~~~~~
/usr/bin/ld: /tmp/ccwRpA2B.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status