Submission #1010224

#TimeUsernameProblemLanguageResultExecution timeMemory
1010224spacewalkerDreaming (IOI13_dreaming)C++17
100 / 100
66 ms12272 KiB
#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; } pair<int, int> find_cc_radius_diam(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 rad = diameter; for (; v2 != v1; v2 = par[v2]) { rad = min(rad, max(dist[v2], diameter - dist[v2])); } return {rad, diameter}; } 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); int ans = 0; for (int i = 0; i < N; ++i) { if (visited[i] != -1) continue; auto [r, d] = find_cc_radius_diam(graph, i, visited, par); radius.push_back(r); ans = max(ans, d); } sort(radius.begin(), radius.end(), greater()); if (radius.size() == 1) { return ans; } else if (radius.size() == 2) { return max(ans, radius[0] + radius[1] + L); } else { return max(ans, max(radius[0] + radius[1] + L, radius[1] + radius[2] + 2 * L)); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...