Submission #297571

#TimeUsernameProblemLanguageResultExecution timeMemory
297571Haunted_CppDreaming (IOI13_dreaming)C++17
100 / 100
340 ms24696 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); } } } vector<int> dp_down(MAX_N); vector<int> dp_up(MAX_N); int calc_down(const vector<vector<pair<int, int>>>& graph, int node, int p) { for (auto [to, cost] : graph[node]) { if (to != p) { dp_down[node] = max(dp_down[node], calc_down(graph, to, node) + cost); } } return dp_down[node]; } void calc_up(const vector<vector<pair<int, int>>>& graph, int node, int p) { int best = -1, second_best = -1; for (auto [to, cost] : graph[node]) { if (to != p) { const int w = dp_down[to] + cost; if (w > best) { swap(best, second_best); best = w; } else { second_best = max(second_best, w); } } } for (auto [to, cost] : graph[node]) { if (to != p) { const int w = dp_down[to] + cost; dp_up[to] = max({dp_up[to], cost + dp_up[node], cost + (w == best ? second_best : best)}); calc_up(graph, to, node); } } } pair<int, int> opt = {2e9, 2e9}; void find_optimal(const vector<vector<pair<int, int>>>& graph, int node, int p) { opt = min(opt, { max(dp_down[node], dp_up[node]), node } ); //~ cout << node << ' ' << dp_down[node] << ' ' << dp_up[node] << '\n'; for (auto [to, cost] : graph[node]) { if (to != p) { find_optimal(graph, to, node); } } //~ return mn; } tuple<int, 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 longest_path = best_way.first; calc_down(graph, node, -1); calc_up(graph, node, -1); opt = {2e9, 2e9}; find_optimal(graph, node, -1); //~ cout << opt.second << ' ' << longest_path << '\n'; return make_tuple(opt.first, opt.second, longest_path); } int travelTime(int N, int M, int L, int A[], int B[], int T[]) { for (int i = 0; i < M; i++) { //~ cout << A[i] << ' ' << B[i] << ' ' << T[i] << '\n'; 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; vector<tuple<int, int, int>> best; for (int i = 0; i < N; i++) { if (!vis[i]) { best.emplace_back(find_center(g, i)); //~ auto w = ; //~ if (nxt != -1) { //~ cout << nxt << ' ' << w.first << '\n'; //~ tree[nxt].emplace_back(w.first, L); //~ tree[w.first].emplace_back(nxt, L); //~ } //~ nxt = w.first; //~ return 0; } } sort(best.begin(), best.end(), [&](auto a, auto b) { return get<0>(a) < get<0>(b); }); const int root = get<1>(best.back()); for (int i = 0; i < (int) best.size() - 1; i++) { //~ cout << get<1>(best[i - 1]) << ' ' << get<1>(best[i]) << '\n'; tree[ get<1>(best[i]) ].emplace_back( root , L); tree[ root ].emplace_back( get<1>(best[i]), L); } return get<2>(find_center(tree, 0)); }

Compilation message (stderr)

dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:95:7: warning: unused variable 'nxt' [-Wunused-variable]
   95 |   int nxt = -1;
      |       ^~~
#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...