Submission #885501

#TimeUsernameProblemLanguageResultExecution timeMemory
885501TobIslands (IOI08_islands)C++14
15 / 100
384 ms131072 KiB
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define all(x) x.begin(), x.end() #define pb push_back #define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) using namespace std; typedef pair <ll, ll> pii; const int N = 1e6 + 7; ll n, ro, sp, res; ll a[N], b[N], dp[N][2][2]; int bio[N]; vector <pii> adj[N]; void Merge(pii& x, ll y) { if (y >= x.F) x = {y, x.F}; else x = {x.F, max(x.S, y)}; } void dfs(int x) { pii p1 = {0, 0}, p2 = {0, 0}; for (auto y : adj[x]) { if (y.F == ro) continue; dfs(y.F); Merge(p1, dp[y.F][1][0] + y.S); Merge(p2, dp[y.F][1][1] + y.S); } if (x != sp) { if (x != ro) dp[x][0][0] = p1.F + p1.S; else dp[x][0][1] = p2.F + p2.S; dp[x][1][0] = p1.F; dp[x][1][1] = p2.F; } else { ll alt = dp[ro][1][1] + b[ro]; dp[x][0][0] = max(p1.F + p1.S, alt + p1.F); dp[x][1][0] = max(alt, p1.F); dp[x][1][1] = p1.F; } res = max(res, max(dp[x][0][0], dp[x][0][1])); } int main () { FIO; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; adj[a[i]].pb({i, b[i]}); } ll sum = 0; for (int i = 1; i <= n; i++) { if (bio[i]) continue; int x = a[i], y = i; bio[i] = i; while (!bio[x]) { bio[x] = i; y = x; x = a[x]; } if (bio[x] == i) { ro = y; sp = x; res = 0; dfs(ro); sum += res; } } cout << sum; return 0; }
#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...