Submission #940981

#TimeUsernameProblemLanguageResultExecution timeMemory
940981duckindogDreaming (IOI13_dreaming)C++17
100 / 100
65 ms15564 KiB
#include <bits/stdc++.h> #include "dreaming.h" using namespace std; const int N = 100'000 + 10; vector<pair<int, int>> ad[N]; int f[N], trace[N]; bool mk[N]; vector<int> node; void dfs(int u, int p = -1) { mk[u] = true; node.push_back(u); trace[u] = p; for (const auto& [v, w] : ad[u]) { if (v == p) continue; f[v] = f[u] + w; dfs(v, u); } } pair<int, int> findMid(int u) { int v = u; for (int t = 0; t < 2; ++t) { for (const auto& x : node) f[x] = 0; node.clear(); dfs(u); for (const auto& x : node) if (f[v] < f[x]) v = x; u = v; } int ret = v; while (v != -1) { if (max(f[v], f[u] - f[v]) < max(f[ret], f[u] - f[ret])) ret = v; v = trace[v]; } return {max(f[ret], f[u] - f[ret]), ret}; } int travelTime(int n, int m, int l, int A[],int B[],int T[]) { for (int i = 0; i < m; ++i) { int u = A[i], v = B[i], w = T[i]; ad[u].push_back({v, w}); ad[v].push_back({u, w}); } memset(trace, -1, sizeof trace); vector<pair<int, int>> mid; for (int i = 0; i < n; ++i) { if (mk[i]) continue; mid.push_back(findMid(i)); } sort(mid.begin(), mid.end(), greater<>()); for (const auto& x : mid) { if (x == mid[0]) continue; int u = mid[0].second, v = x.second; ad[u].push_back({v, l}); ad[v].push_back({u, l}); } memset(f, 0, sizeof f); memset(trace, -1, sizeof trace); findMid(0); return *max_element(f, f + n); }
#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...