#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] + 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] = max(_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[]) {
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.back() + f[f.size() - 2] + 2 * L);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'void reroot(int, int)':
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: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: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: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] + w);
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
22492 KB |
Output is correct |
2 |
Correct |
47 ms |
21460 KB |
Output is correct |
3 |
Correct |
34 ms |
21460 KB |
Output is correct |
4 |
Correct |
7 ms |
7260 KB |
Output is correct |
5 |
Correct |
6 ms |
5904 KB |
Output is correct |
6 |
Correct |
12 ms |
8536 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
22492 KB |
Output is correct |
2 |
Correct |
47 ms |
21460 KB |
Output is correct |
3 |
Correct |
34 ms |
21460 KB |
Output is correct |
4 |
Correct |
7 ms |
7260 KB |
Output is correct |
5 |
Correct |
6 ms |
5904 KB |
Output is correct |
6 |
Correct |
12 ms |
8536 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
7388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
22492 KB |
Output is correct |
2 |
Correct |
47 ms |
21460 KB |
Output is correct |
3 |
Correct |
34 ms |
21460 KB |
Output is correct |
4 |
Correct |
7 ms |
7260 KB |
Output is correct |
5 |
Correct |
6 ms |
5904 KB |
Output is correct |
6 |
Correct |
12 ms |
8536 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |