#include <bits/stdc++.h>
using namespace std;
#include "dreaming.h"
vector<pair<int, int>> distances(const vector<vector<pair<int, int>>> &graph, int start, vector<int> &dist, vector<int> &par) {
queue<int> q;
q.push(start);
dist[start] = 0;
vector<pair<int, int>> component;
while (!q.empty()) {
int cur = q.front(); q.pop();
component.emplace_back(dist[cur], cur);
for (auto [u, w] : graph[cur]) {
if (dist[u] != -1) continue;
dist[u] = dist[cur] + w;
par[u] = cur;
q.push(u);
}
}
return component;
}
int find_cc_radius(const vector<vector<pair<int, int>>> &graph, int start, vector<int> &dist, vector<int> &par) {
vector<pair<int, int>> cc = distances(graph, start, dist, par);
int v1 = max_element(cc.begin(), cc.end())->second;
// reset the dist array, so we can run the second BFS
for (auto [_, v] : cc) dist[v] = -1;
cc = distances(graph, v1, dist, par);
int v2 = max_element(cc.begin(), cc.end())->second;
// The "center" of the tree must be on the v1-v2 path.
int diameter = dist[v2];
int ans = diameter;
for (; v2 != v1; v2 = par[v2]) {
ans = min(ans, max(dist[v2], diameter - dist[v2]));
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vector<int> radius;
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < M; ++i) {
graph[A[i]].emplace_back(B[i], T[i]);
graph[B[i]].emplace_back(A[i], T[i]);
}
vector<int> visited(N, -1), par(N, -1);
for (int i = 0; i < N; ++i) {
if (visited[i] != -1) continue;
radius.emplace_back(find_cc_radius(graph, i, visited, par));
}
sort(radius.begin(), radius.end(), greater());
if (radius.size() == 1) {
return radius[0];
} else {
return max(radius[0] + radius[1] + L, radius[1] + radius[2] + 2 * L);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
10508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
10508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
6492 KB |
Output is correct |
2 |
Correct |
20 ms |
6480 KB |
Output is correct |
3 |
Correct |
28 ms |
6484 KB |
Output is correct |
4 |
Correct |
27 ms |
6408 KB |
Output is correct |
5 |
Correct |
25 ms |
6492 KB |
Output is correct |
6 |
Correct |
24 ms |
6868 KB |
Output is correct |
7 |
Correct |
21 ms |
6748 KB |
Output is correct |
8 |
Correct |
22 ms |
6352 KB |
Output is correct |
9 |
Correct |
23 ms |
6228 KB |
Output is correct |
10 |
Correct |
26 ms |
6616 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
12 ms |
4056 KB |
Output is correct |
13 |
Correct |
12 ms |
4056 KB |
Output is correct |
14 |
Correct |
11 ms |
4056 KB |
Output is correct |
15 |
Correct |
11 ms |
4056 KB |
Output is correct |
16 |
Correct |
18 ms |
4128 KB |
Output is correct |
17 |
Correct |
12 ms |
4056 KB |
Output is correct |
18 |
Correct |
12 ms |
4056 KB |
Output is correct |
19 |
Correct |
12 ms |
4144 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
10508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |