# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
66543 |
2018-08-11T11:11:36 Z |
aome |
Islands (IOI08_islands) |
C++11 |
|
521 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 |
46 ms |
47356 KB |
Output is correct |
2 |
Correct |
46 ms |
47496 KB |
Output is correct |
3 |
Correct |
43 ms |
47552 KB |
Output is correct |
4 |
Correct |
50 ms |
47588 KB |
Output is correct |
5 |
Correct |
49 ms |
47636 KB |
Output is correct |
6 |
Correct |
47 ms |
47636 KB |
Output is correct |
7 |
Correct |
46 ms |
47636 KB |
Output is correct |
8 |
Correct |
46 ms |
47640 KB |
Output is correct |
9 |
Correct |
43 ms |
47728 KB |
Output is correct |
10 |
Correct |
48 ms |
47728 KB |
Output is correct |
11 |
Correct |
41 ms |
47728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
47784 KB |
Output is correct |
2 |
Correct |
49 ms |
47796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
47820 KB |
Output is correct |
2 |
Correct |
48 ms |
48104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
48928 KB |
Output is correct |
2 |
Correct |
72 ms |
50404 KB |
Output is correct |
3 |
Correct |
57 ms |
50404 KB |
Output is correct |
4 |
Correct |
48 ms |
50404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
52432 KB |
Output is correct |
2 |
Correct |
100 ms |
56300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
64196 KB |
Output is correct |
2 |
Correct |
177 ms |
70572 KB |
Output is correct |
3 |
Correct |
209 ms |
75604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
86352 KB |
Output is correct |
2 |
Correct |
340 ms |
99344 KB |
Output is correct |
3 |
Correct |
399 ms |
115220 KB |
Output is correct |
4 |
Correct |
420 ms |
126824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
403 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 |
521 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 |
- |