#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] + 2 * L);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'void reroot(int, int)':
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: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: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: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);
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
21972 KB |
Output is correct |
2 |
Correct |
52 ms |
20992 KB |
Output is correct |
3 |
Correct |
36 ms |
19672 KB |
Output is correct |
4 |
Correct |
6 ms |
3420 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
12 ms |
5208 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
21972 KB |
Output is correct |
2 |
Correct |
52 ms |
20992 KB |
Output is correct |
3 |
Correct |
36 ms |
19672 KB |
Output is correct |
4 |
Correct |
6 ms |
3420 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
12 ms |
5208 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
6368 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
21972 KB |
Output is correct |
2 |
Correct |
52 ms |
20992 KB |
Output is correct |
3 |
Correct |
36 ms |
19672 KB |
Output is correct |
4 |
Correct |
6 ms |
3420 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
12 ms |
5208 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |