# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
63753 |
2018-08-02T17:06:53 Z |
aome |
Islands (IOI08_islands) |
C++17 |
|
612 ms |
132096 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const long long INF = 1e18;
int n;
int par[N];
int deg[N];
int a[N], w[N];
long long mx, res;
long long f[N], g[N];
long long sum[N];
vector<int> G[N], vec[N];
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
void join(int u, int v) { par[find(u)] = find(v); }
void solve(vector<int> &vec) {
queue<int> qu;
for (auto i : vec) {
deg[i] = G[i].size();
if (!deg[i]) qu.push(i);
}
while (qu.size()) {
int u = qu.front(); qu.pop();
if (!--deg[a[u]]) qu.push(a[u]);
f[a[u]] = max(f[a[u]], f[u] + w[u]);
}
vector<int> cir;
for (auto i : vec) {
if (!deg[i]) continue;
int u = i, prv = 0;
while (1) {
cir.push_back(u), u = a[u];
if (u == i) break;
}
break;
}
mx = 0;
for (auto i : cir) {
stack<int> st; st.push(i);
while (st.size()) {
int u = st.top(); st.pop();
mx = max(mx, max(f[u], g[u]));
long long mx1, mx2;
mx1 = mx2 = -INF;
for (auto v : G[u]) {
if (deg[v]) continue;
if (mx1 < f[v] + w[v]) mx2 = mx1, mx1 = f[v] + w[v];
else if (mx2 < f[v] + w[v]) mx2 = f[v] + w[v];
}
for (auto v : G[u]) {
if (deg[v]) continue;
g[v] = g[u] + w[v];
if (mx1 == f[v] + w[v]) g[v] = max(g[v], mx2 + w[v]);
else g[v] = max(g[v], mx1 + w[v]);
st.push(v);
}
}
}
int sz = cir.size();
for (int i = 1; i <= sz; ++i) {
sum[i] = sum[i - 1] + w[cir[i - 1]];
}
long long mx1 = -INF, mx2 = -INF;
for (int i = 1; i <= sz; ++i) {
int u = cir[i - 1];
mx = max(mx, f[u] + sum[i - 1] + mx1);
mx = max(mx, f[u] - sum[i - 1] + sum[sz] + mx2);
mx1 = max(mx1, f[u] - sum[i - 1]);
mx2 = max(mx2, f[u] + sum[i - 1]);
}
res += mx;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) par[i] = i;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> w[i];
join(i, a[i]), G[a[i]].push_back(i);
}
for (int i = 1; i <= n; ++i) vec[find(i)].push_back(i);
for (int i = 1; i <= n; ++i) if (find(i) == i) solve(vec[find(i)]);
cout << res;
}
Compilation message
islands.cpp: In function 'void solve(std::vector<int>&)':
islands.cpp:36:14: warning: unused variable 'prv' [-Wunused-variable]
int u = i, prv = 0;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
47352 KB |
Output is correct |
2 |
Correct |
47 ms |
47472 KB |
Output is correct |
3 |
Correct |
48 ms |
47472 KB |
Output is correct |
4 |
Correct |
46 ms |
47520 KB |
Output is correct |
5 |
Correct |
54 ms |
47520 KB |
Output is correct |
6 |
Correct |
42 ms |
47520 KB |
Output is correct |
7 |
Correct |
45 ms |
47556 KB |
Output is correct |
8 |
Correct |
47 ms |
47624 KB |
Output is correct |
9 |
Correct |
46 ms |
47628 KB |
Output is correct |
10 |
Correct |
45 ms |
47704 KB |
Output is correct |
11 |
Correct |
55 ms |
47704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
47780 KB |
Output is correct |
2 |
Correct |
52 ms |
47824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
47924 KB |
Output is correct |
2 |
Correct |
54 ms |
48188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
49056 KB |
Output is correct |
2 |
Correct |
80 ms |
50532 KB |
Output is correct |
3 |
Correct |
66 ms |
50532 KB |
Output is correct |
4 |
Correct |
53 ms |
50532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
52672 KB |
Output is correct |
2 |
Correct |
116 ms |
56344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
64448 KB |
Output is correct |
2 |
Correct |
172 ms |
70716 KB |
Output is correct |
3 |
Correct |
236 ms |
75884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
299 ms |
86480 KB |
Output is correct |
2 |
Correct |
369 ms |
99564 KB |
Output is correct |
3 |
Correct |
408 ms |
115348 KB |
Output is correct |
4 |
Correct |
470 ms |
127080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
446 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
612 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
2 |
Halted |
0 ms |
0 KB |
- |