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 "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> adj;
vector<int> mark;
vector<int> high, _high;
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] + w));
}
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] + w));
}
reverse(R.begin(), R.end());
for(int i = 0; i < k.size(); i ++) {
auto [v, w] = k[i];
if(v == par) continue ;
_high[v] = _high[u] + w;
if(i) _high[v] = max(_high[v], L[i - 1] + w);
if(i + 1 < R.size()) _high[v] = max(_high[v], R[i + 1] + w);
reroot(v, u);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj.resize(N);
mark.assign(N, 0);
high.resize(N);
_high.resize(N);
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);
_high[i] = 0;
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.back() + f[f.size() - 2] + L);
}
if(f.size() > 2) {
ans = max(ans, f[f.size() - 2] + f[f.size() - 3] + 2 * L);
}
return ans;
}
Compilation message (stderr)
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for(auto [v, w] : adj[u]) {
| ^
dreaming.cpp: In function 'void reroot(int, int)':
dreaming.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for(auto [v, w] : adj[u]) {
| ^
dreaming.cpp:31: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]
31 | for(int i = 0; i < k.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:32:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | auto [v, w] = k[i];
| ^
dreaming.cpp:36: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]
36 | for(int i = 0; i < k.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:37:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | auto [v, w] = k[k.size() - 1 - i];
| ^
dreaming.cpp:43: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]
43 | for(int i = 0; i < k.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:44:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | auto [v, w] = k[i];
| ^
dreaming.cpp:49:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if(i + 1 < R.size()) _high[v] = max(_high[v], R[i + 1] + w);
| ~~~~~~^~~~~~~~~~
# | 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... |