Submission #297445

#TimeUsernameProblemLanguageResultExecution timeMemory
297445Haunted_CppDreaming (IOI13_dreaming)C++17
0 / 100
129 ms19980 KiB
#include <bits/stdc++.h> #include "dreaming.h" using namespace std; const int MAX_N = 1e5 + 5; vector<vector<pair<int, int>>> g(MAX_N); vector<vector<pair<int, int>>> tree(MAX_N); vector<int> par(MAX_N); bitset<MAX_N> vis; pair<int, int> best_way = {-1, -1}; void find_diam(const vector<vector<pair<int, int>>>& graph, int node, int p, int w) { par[node] = p; vis[node] = 1; best_way = max(best_way, {w, node}); for (auto [to, cost] : graph[node]) { if (to != p) { find_diam(graph, to, node, w + cost); } } } int big(const vector<vector<pair<int, int>>>& graph, int node, int p, int w) { int mx = w; for (auto [to, cost] : graph[node]) { if (to != p) { mx = max(mx, big(graph, to, node, w + cost)); } } return mx; } pair<int, int> find_center(const vector<vector<pair<int, int>>>& graph, int node) { best_way = {-1, -1}; find_diam(graph, node, -1, 0); int L = best_way.second; best_way = {-1, -1}; find_diam(graph, L, -1, 0); int R = best_way.second; int longest_path = best_way.first; vector<int> path; while(R != L) { path.emplace_back(R); R = par[R]; } path.emplace_back(R); const int mid = (int) path.size() / 2; if ( (int) path.size() & 1) { return {path[mid], longest_path}; } auto A = make_pair(big(graph, path[mid], -1, 0), path[mid]); auto B = make_pair(big(graph, path[mid - 1], -1, 0), path[mid - 1]); return { (A.first < B.first ? A.second : B.second), longest_path}; } int travelTime(int N, int M, int L, int A[], int B[], int T[]) { for (int i = 0; i < M; i++) { g[A[i]].emplace_back(B[i], T[i]); g[B[i]].emplace_back(A[i], T[i]); tree[A[i]].emplace_back(B[i], T[i]); tree[B[i]].emplace_back(A[i], T[i]); } int nxt = -1; for (int i = 0; i < N; i++) { if (!vis[i]) { auto w = find_center(g, i); if (nxt != -1) { tree[nxt].emplace_back(w.first, L); tree[w.first].emplace_back(nxt, L); } nxt = w.first; } } return find_center(tree, 0).second; }
#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...