제출 #388417

#제출 시각아이디문제언어결과실행 시간메모리
388417osaaateiasavtnlIslands (IOI08_islands)C++14
90 / 100
1042 ms131076 KiB
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> #include <fstream> #include <stack> using namespace std; #define ii pair <int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcount #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock()/CLOCKS_PER_SEC const int N = 1e6 + 7; const ll INF = 1e18 + 7; vector <ii> g[N]; bool inc[N]; int pw[N]; int n; void bfs() { queue <int> q; for (int i = 1; i <= n; ++i) { pw[i] = g[i].size(); if (pw[i] == 1) q.push(i); inc[i] = 1; } while (q.size()) { int u = q.front(); q.pop(); inc[u] = 0; for (auto e : g[u]) { int v = e.f; --pw[v]; if (pw[v] == 1) q.push(v); } } } int to[N], len[N]; ll h[N], diam[N]; pair <ll, ll> dfs1(int u, int p) { pair <ll, ll> ans = {0, 0}; for (auto e : g[u]) { int v = e.f; if (v == p || inc[v]) continue; auto t = dfs1(v, u); ans.s = max(ans.s, t.s); ans.s = max(ans.s, ans.f + t.f + e.s); ans.f = max(ans.f, t.f + e.s); } ans.s = max(ans.s, ans.f); return ans; } ll pref[N]; int r[N]; int cl[N]; ll solve(int n) { ll ans = 0; for (int i = 0; i < n; ++i) ans = max(ans, diam[cl[i]]); for (int i = 1; i < n; ++i) pref[i] = pref[i - 1] + len[cl[i - 1]]; stack <int> s; for (int i = 0; i < n; ++i) r[i] = n; for (int i = 0; i < n; ++i) { while (s.size() && h[cl[s.top()]] - pref[s.top()] <= h[cl[i]] - pref[i]) { r[s.top()] = i; s.pop(); } s.push(i); } int ptr = 0; ll cur = 0; auto get = [&] (int i) { if (i < n) return cl[i]; else return cl[i - n]; }; for (int i = 1; i < 2 * n - 1; ++i) { cur += len[get(i - 1)]; ptr = max(ptr, i - n + 1); while (r[ptr] != n && r[ptr] < i) ptr = r[ptr]; ans = max(ans, h[get(i)] + cur + h[get(ptr)] - pref[ptr]); } return ans; } bool used[N]; signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif cin >> n; for (int i = 1; i <= n; ++i) { cin >> to[i] >> len[i]; g[i].app(mp(to[i], len[i])); g[to[i]].app(mp(i, len[i])); } bfs(); for (int i = 1; i <= n; ++i) if (inc[i]) tie(h[i], diam[i]) = dfs1(i, i); ll ans = 0; for (int i = 1; i <= n; ++i) { if (!used[i] && inc[i]) { int u = i; int len = 0; while (!used[u]) { used[u] = 1; cl[len++] = u; u = to[u]; } ans += solve(len); } } 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...