# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
650679 | Spade1 | Islands (IOI08_islands) | C++14 | 344 ms | 131072 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 1e6 + 10;
vector<pair<int, pll>> adj[N];
bool vis[N];
int cycle;
vector<pll> v;
ll dp[N], qs[N], ans, cur;
pll dfs2(int i, int prt1, int prt2) {
ll mx_w = 0, mx = i;
for (auto [j, w] : adj[i]) {
if (j == prt1 || j == prt2) continue;
pll ret = dfs2(j, i, 0);
if (ret.nd + w.nd > mx_w) {
mx_w = ret.nd + w.nd;
mx = ret.st;
}
}
return {mx, mx_w};
}
int dfs(int i, int ed, int prt) {
if (vis[i]) {
cycle = prt;
return i;
}
vis[i] = 1;
int ret = 0, K, W;
for (auto [j, w] : adj[i]) {
if (j == prt && ed == w.st) continue;
if (ret != 0 && vis[j]) continue;
int k = dfs(j, w.st, i);
if (k) {
ret = k;
K = j;
W = w.nd;
}
}
if (ret == i) {
auto [val, w] = dfs2(i, K, cycle);
v.pb({w, W});
return 0;
}
else if (ret) {
auto [val, w] = dfs2(i, prt, K);
v.pb({w, W});
}
return ret;
}
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) {
int b, w; cin >> b >> w;
adj[i].pb({b ,{i, w}});
adj[b].pb({i, {i, w}});
}
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
dfs(i, 0, 0);
int m = v.size();
for (int j = 0; j <= m+1; ++j) qs[j] = 0, dp[j] = 0;
for (int j = 1; j <= m; ++j) {
qs[j] = qs[j-1] + v[j-1].nd;
dp[j] = qs[j] + v[j-1].st;
}
for (int j = m-1; j > 0; --j) dp[j] = max(dp[j], dp[j+1]);
for (int j = 1; j < m; ++j) {
cur = max(cur, v[j-1].st - qs[j] + dp[j+1]);
}
for (int j = 0; j <= m+1; ++j) dp[j] = 0;
for (int j = m; j >= 1; --j) {
dp[j] = qs[j] + v[j-1].st;
}
for (int j = 2; j <= m; ++j) dp[j] = max(dp[j], dp[j-1]);
for (int j = 2; j <= m; ++j) {
cur = max(cur, qs[m] + v[j-1].st - qs[j] + dp[j-1]);
}
ans += cur;
v.clear();
cur = 0, cycle = 0;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |