# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
66541 |
2018-08-11T11:11:05 Z |
aome |
Islands (IOI08_islands) |
C++11 |
|
590 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 |
47376 KB |
Output is correct |
2 |
Correct |
50 ms |
47464 KB |
Output is correct |
3 |
Correct |
49 ms |
47464 KB |
Output is correct |
4 |
Correct |
48 ms |
47560 KB |
Output is correct |
5 |
Correct |
50 ms |
47560 KB |
Output is correct |
6 |
Correct |
50 ms |
47608 KB |
Output is correct |
7 |
Correct |
48 ms |
47612 KB |
Output is correct |
8 |
Correct |
55 ms |
47684 KB |
Output is correct |
9 |
Correct |
44 ms |
47784 KB |
Output is correct |
10 |
Correct |
55 ms |
47784 KB |
Output is correct |
11 |
Correct |
49 ms |
47784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47784 KB |
Output is correct |
2 |
Correct |
51 ms |
47868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
48060 KB |
Output is correct |
2 |
Correct |
49 ms |
48216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
49072 KB |
Output is correct |
2 |
Correct |
69 ms |
50528 KB |
Output is correct |
3 |
Correct |
58 ms |
50528 KB |
Output is correct |
4 |
Correct |
51 ms |
50528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
52644 KB |
Output is correct |
2 |
Correct |
102 ms |
56444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
64412 KB |
Output is correct |
2 |
Correct |
158 ms |
70720 KB |
Output is correct |
3 |
Correct |
218 ms |
75648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 ms |
86528 KB |
Output is correct |
2 |
Correct |
333 ms |
99492 KB |
Output is correct |
3 |
Correct |
400 ms |
115384 KB |
Output is correct |
4 |
Correct |
418 ms |
126944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
421 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 |
590 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 |
- |