# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
63746 |
2018-08-02T16:59:01 Z |
aome |
Islands (IOI08_islands) |
C++11 |
|
1421 ms |
132096 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const long long INF = 1e18;
int n;
int deg[N];
int a[N], w[N];
bool visit[N];
long long mx, res;
long long f[N], g[N];
vector<int> G[N], vec;
void dfs1(int u, int p) {
for (auto v : G[u]) {
if (deg[v] == 2 || v == p) continue;
dfs1(v, u), f[u] = max(f[u], f[v] + w[v]);
}
}
void dfs2(int u, int p) {
mx = max(mx, max(f[u], g[u]));
long long mx1, mx2;
mx1 = mx2 = -INF;
for (auto v : G[u]) {
if (deg[v] == 2 || v == p) 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] == 2 || v == p) 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]);
dfs2(v, u);
}
}
void solve() {
queue<int> qu;
for (auto i : vec) {
deg[i] = G[i].size();
if (deg[i] == 1) qu.push(i);
}
while (qu.size()) {
int u = qu.front(); qu.pop();
if (--deg[a[u]] == 1) qu.push(a[u]);
}
vector<int> cir;
for (auto i : vec) {
if (deg[i] != 2) 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) dfs1(i, i), dfs2(i, i);
int sz = cir.size();
long long sumAll = 0;
for (auto i : cir) sumAll += w[i];
long long sum = 0;
long long mx1 = -INF, mx2 = -INF;
for (auto u : cir) {
mx = max(mx, f[u] + sum + mx1);
mx = max(mx, f[u] - sum + sumAll + mx2);
mx1 = max(mx1, f[u] - sum);
mx2 = max(mx2, f[u] + sum);
sum += w[u];
}
res += mx;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> w[i];
G[a[i]].push_back(i);
G[i].push_back(a[i]);
}
for (int i = 1; i <= n; ++i) {
if (visit[i]) continue;
vec.clear();
queue<int> qu;
qu.push(i), visit[i] = 1;
while (qu.size()) {
int u = qu.front(); qu.pop();
vec.push_back(u);
for (auto v : G[u]) {
if (!visit[v]) {
visit[v] = 1, qu.push(v);
}
}
}
solve();
}
cout << res;
}
Compilation message
islands.cpp: In function 'void solve()':
islands.cpp:55:14: warning: unused variable 'prv' [-Wunused-variable]
int u = i, prv = 0;
^~~
islands.cpp:64:6: warning: unused variable 'sz' [-Wunused-variable]
int sz = cir.size();
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
23800 KB |
Output is correct |
2 |
Correct |
29 ms |
24040 KB |
Output is correct |
3 |
Correct |
25 ms |
24172 KB |
Output is correct |
4 |
Correct |
25 ms |
24172 KB |
Output is correct |
5 |
Correct |
30 ms |
24172 KB |
Output is correct |
6 |
Correct |
29 ms |
24172 KB |
Output is correct |
7 |
Correct |
31 ms |
24172 KB |
Output is correct |
8 |
Correct |
30 ms |
24172 KB |
Output is correct |
9 |
Correct |
29 ms |
24264 KB |
Output is correct |
10 |
Correct |
31 ms |
24264 KB |
Output is correct |
11 |
Correct |
31 ms |
24264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
24284 KB |
Output is correct |
2 |
Correct |
29 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24488 KB |
Output is correct |
2 |
Correct |
29 ms |
24680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
25732 KB |
Output is correct |
2 |
Correct |
60 ms |
27504 KB |
Output is correct |
3 |
Correct |
43 ms |
27504 KB |
Output is correct |
4 |
Correct |
35 ms |
27504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
29496 KB |
Output is correct |
2 |
Correct |
76 ms |
33212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
43168 KB |
Output is correct |
2 |
Correct |
235 ms |
44864 KB |
Output is correct |
3 |
Correct |
217 ms |
53492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
64004 KB |
Output is correct |
2 |
Correct |
389 ms |
80828 KB |
Output is correct |
3 |
Correct |
407 ms |
83292 KB |
Output is correct |
4 |
Correct |
460 ms |
106208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
601 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 |
1421 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 |
- |