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 <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 if (radius.size() == 2) {
return radius[0] + radius[1] + L;
} else {
return max(radius[0] + radius[1] + L, radius[1] + radius[2] + 2 * L);
}
}
# | 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... |