#include <bits/stdc++.h>
using namespace std;
using ll = long long;
array<int, 2> dmax;
vector<int> id, d;
vector<vector<int>> cc;
vector<vector<pair<int, int>>> g;
void comps(int v) {
if (id[v] != -1) return;
id[v] = cc.size() - 1;
cc.back().push_back(v);
for (auto [u, w] : g[v])
comps(u);
}
void dfs(int v, int p) {
if (d[v] > dmax[1]) dmax = {v, d[v]};
for (auto [u, w] : g[v]) {
if (u == p) continue;
d[u] = d[v] + w;
dfs(u, v);
}
}
int diam(int v) {
dmax = {v, 0}; dfs(v, -1);
v = dmax[0]; d[v] = 0;
dmax = {v, 0}; dfs(v, -1);
return dmax[1];
}
int coda(int v) {
// modo stupido O(n^2)
int ans = 1e9;
for (int u : cc[id[v]]) {
fill(d.begin(), d.end(), 0);
dfs(u, -1);
ans = min(ans, *max_element(d.begin(), d.end()));
}
// cout << "coda[" << v << "] = " << ans << "\n";
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
g.resize(N);
d.resize(N);
id.resize(N, -1);
for (int i = 0; i < M; ++i) {
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
for (int i = 0; i < N; ++i) {
if (id[i] == -1) {
cc.push_back(vector<int>(0));
comps(i);
}
}
int ans = 0;
for (int i = 0; i < int(cc.size()); ++i)
ans = max(ans, diam(cc[i][0]));
array<int, 3> o{};
for (int i = 0; i < int(cc.size()); ++i) {
o[2] = max(o[2], coda(cc[i][0]));
if (o[1] < o[2]) swap(o[1], o[2]);
if (o[0] < o[1]) swap(o[0], o[1]);
}
// cout << o[0] << " " << o[1] << " " << o[2] << "\n";
if (int(cc.size()) == 1) return ans;
ans = max(ans, o[0] + o[1] + L);
if (int(cc.size()) == 2) return ans;
ans = max(ans, o[1] + o[2] + 2*L);
return ans;
}
Compilation message
dreaming.cpp: In function 'void comps(int)':
dreaming.cpp:14:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
14 | for (auto [u, w] : g[v])
| ^
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:20:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [u, w] : g[v]) {
| ^
/usr/bin/ld: /tmp/ccdcMtOS.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status