#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN = 100005;
vector<vector<pair<int, int>>> adj(mxN);
int mark[mxN];
int high[mxN], _high[mxN];
vector<int> vertex;
void dfs(int u, int par) {
vertex.push_back(u);
mark[u] = 1;
high[u] = 0;
for(auto [v, w] : adj[u]) {
if(v == par) continue ;
dfs(v, u);
high[u] = max(high[u], high[v] + w);
}
}
void reroot(int u, int par) {
vector<pair<int, int>> k;
vector<int> L, R;
for(auto [v, w] : adj[u]) {
if(v == par) continue ;
k.push_back({v, w});
}
for(int i = 0; i < k.size(); i ++) {
auto [v, w] = k[i];
L.push_back(max((!L.empty() ? L.back() : 0), high[v] + 1));
}
for(int i = 0; i < k.size(); i ++) {
auto [v, w] = k[k.size() - 1 - i];
R.push_back(max((!R.empty() ? R.back() : 0), high[v] + 1));
}
reverse(R.begin(), R.end());
for(int i = 0; i < k.size(); i ++) {
int v = k[i];
if(v == par) continue ;
_high[v] = max(_high[v], _high[u] + 1);
if(i) _high[v] = max(_high[v], L[i - 1] + 1);
if(i + 1 < R.size()) _high[v] = max(_high[v], R[i + 1] + 1);
reroot(v, u);
}
}
int travelTime(int N, int M, int L, vector<int> A, vector<int> B, vector<int> T) {
for(int i = 0; i < M; i ++) {
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
vector<int> f;
int ans = 0;
for(int i = 0; i < N; i ++) {
if(!mark[i]) {
dfs(i, i);
reroot(i, i);
int timer = max(high[vertex.back()], _high[vertex.back()]);
for(int x : vertex) {
ans = max(ans, high[x] + _high[x]);
timer = min(timer, max(high[x], _high[x]));
}
f.push_back(timer);
vertex.clear();
}
}
sort(f.begin(), f.end());
if(f.size() > 1) {
ans = max(ans, f[f.back()] + f[f.size() - 2] + L);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:17:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for(auto [v, w] : adj[u]) {
| ^
dreaming.cpp: In function 'void reroot(int, int)':
dreaming.cpp:28:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
28 | for(auto [v, w] : adj[u]) {
| ^
dreaming.cpp:33:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i = 0; i < k.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:34:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | auto [v, w] = k[i];
| ^
dreaming.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < k.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:39:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | auto [v, w] = k[k.size() - 1 - i];
| ^
dreaming.cpp:45:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i < k.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:46:19: error: cannot convert '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} to 'int' in initialization
46 | int v = k[i];
| ^
dreaming.cpp:51:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if(i + 1 < R.size()) _high[v] = max(_high[v], R[i + 1] + 1);
| ~~~~~~^~~~~~~~~~