제출 #548389

#제출 시각아이디문제언어결과실행 시간메모리
548389lorenzoferrariIslands (IOI08_islands)C++17
64 / 100
370 ms131072 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O3") using namespace std; using LL = long long; #define watch(x) cerr << (#x) << " is " << x << endl vector<bool> vis, cycle; vector<array<int, 2>> nxt; vector<LL> dl; vector<LL> f; LL max_path(const vector<int>& c, const vector<vector<array<int, 3>>>& adj) { auto find_cycle = [&](auto&& self, int v, int pid = -1) -> bool { if (vis[v]) return cycle[v] = true; vis[v] = true; for (auto [u, w, eid] : adj[v]) { if (eid == pid) continue; bool works = self(self, u, eid); if (works) { nxt[v] = {u, w}; return cycle[v] ? false : cycle[v] = true; } } return false; }; find_cycle(find_cycle, c[0]); auto path_in_tree = [&](auto&& self, int v, int p = -1) -> LL { LL ans = 0; for (auto [u, w, eid] : adj[v]) { if (u == p || cycle[u]) continue; ans = max(ans, w + self(self, u, v)); } return ans; }; int start = -1; for (int i : c) { if (cycle[i]) { start = i; f[i] = path_in_tree(path_in_tree, i); } } vector<LL> fs; vector<LL> ds; { int v = start; LL len = 0; do { fs.push_back(f[v]); ds.push_back(len); auto [nv, lw] = nxt[v]; len += lw; v = nv; } while (v != start); ds.push_back(len); } int n = fs.size(); for (int i = 0; i < n; ++i) fs.push_back(fs[i]); for (int i = 1; i < n; ++i) ds.push_back(ds.back() + ds[i] - ds[i-1]); LL ans = 0; /* find i,j to maximize fs[i] + fs[j] + d(i->j) j < i+n ! fs[i] + fs[j] + ds[j] - ds[i] fs[i] - ds[i] + max(fs[j] + ds[j]) */ LL best = fs[0] + ds[0]; LL until = -1; for (int i = 0; i < n; ++i) { for (int j = until+1; j < i+n; ++j) { best = max(best, fs[j] + ds[j]); until = j; } ans = max(ans, fs[i] - ds[i] + best); } return ans; } int main() { #ifdef LORENZO freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<vector<array<int, 3>>> adj(n); for (int i = 0; i < n; ++i) { int a; cin >> a; --a; int w; cin >> w; adj[i].push_back({a, w, i}); adj[a].push_back({i, w, i}); } vis.assign(n, false); vector<vector<int>> cc; auto dfs = [&](auto&& self, int v) -> void { vis[v] = true; cc.back().push_back(v); for (auto [u, _, __] : adj[v]) { if (!vis[u]) { self(self, u); } } }; for (int i = 0; i < n; ++i) { if (!vis[i]) { cc.push_back(vector<int>(0)); dfs(dfs, i); } } f.assign(n, 0); dl.assign(n, 1e18); nxt.assign(n, {-1,-1}); vis.assign(n, false); cycle.assign(n, false); LL ans = 0; for (auto& c : cc) { ans += max_path(c, adj); c.clear(); } cout << ans << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...