# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
629738 | dooompy | Islands (IOI08_islands) | C++17 | 14 ms | 23788 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>
using namespace std;
using ll = long long;
struct edge {
int to; ll cost; int idx;
};
vector<edge> adj[1000005];
bitset<1000005> seen, seenedge, inloop;
int par[1000005];
int cyclestart, cycleend;
void dfs(int node) {
seen[node] = true;
for (auto nxt : adj[node]) {
if (seenedge[nxt.idx]) continue;
seenedge[nxt.idx] = true;
if (seen[nxt.to]) {
// cycle found
cyclestart = node, cycleend = nxt.to;
} else {
par[nxt.to] = node;
dfs(nxt.to);
}
}
}
ll globmax = 0;
ll dfs_tree(int node, int par = -1) {
ll ans = 0;
for (auto nxt : adj[node]) {
if (nxt.to == par) continue;
if (inloop[nxt.to]) continue;
ll temp = dfs_tree(nxt.to, node);
globmax = max(globmax, ans + temp + nxt.cost);
ans = max(ans, temp + nxt.cost);
}
return ans;
}
int main() {
freopen("isl.in", "r", stdin); freopen("isl.out", "w", stdout);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
int to; ll c; cin >> to >> c;
adj[i].push_back({to, c, i});
adj[to].push_back({i, c, i});
}
ll total = 0;
for (int i = 1; i <= n; i++) {
if (seen[i]) continue;
dfs(i);
vector<int> loop;
while (cyclestart != cycleend) {
loop.push_back(cyclestart);
cyclestart = par[cyclestart];
}
loop.push_back(cyclestart);
for (auto cur : loop) {
inloop[cur] = true;
}
ll ans = 0;
ll prevInMax = 0;
ll prevOutMax = 0;
map<int, ll> cursum;
ll totalsum = 0;
map<int, bool> seensecond;
for (int i = 0; i <= loop.size(); i++) {
if (i == 0) continue;
if (i == loop.size()) {
for (auto nxt : adj[loop[0]]) {
if (nxt.to == loop.back() && !seensecond[nxt.idx]) {
totalsum += nxt.cost;
break;
}
}
break;
}
for (auto nxt : adj[loop[i]]) {
if (nxt.to == loop[i-1] && !seensecond[nxt.idx]) {
seensecond[nxt.idx] = true;
cursum[loop[i]] += nxt.cost;
totalsum += nxt.cost;
break;
}
}
cursum[loop[i]] += cursum[loop[i-1]];
}
for (int i = 0; i < loop.size(); i++) {
globmax = 0;
ll tree = dfs_tree(loop[i]);
if (i > 0) {
ans = max({ans, tree + cursum[loop[i]] + prevInMax, totalsum + prevOutMax - cursum[loop[i]] + tree});
}
ans = max(ans, globmax);
prevInMax = max(prevInMax, tree - cursum[loop[i]]);
prevOutMax = max(prevOutMax, tree + cursum[loop[i]]);
}
total += ans;
}
cout << total << endl;
}
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... |