# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
998702 |
2024-06-14T14:26:05 Z |
Ghetto |
Islands (IOI08_islands) |
C++17 |
|
1191 ms |
131072 KB |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pil = pair<int, lint>;
using pli = pair<lint, int>;
const int MAX_N = 1e6 + 5;
int n;
int to[MAX_N];
vector<pil> from[MAX_N];
bool seen[MAX_N];
void dfs1(int s) {
stack<int> ord; ord.push(s);
while (ord.size()) {
int u = ord.top(); ord.pop();
seen[u] = true;
if (!seen[to[u]]) ord.push(to[u]);
for (pil x : from[u])
if (!seen[x.first]) ord.push(x.first);
}
}
vector<int> cyc; // Reverse order btw
bool in_cyc[MAX_N];
bool vis[MAX_N];
void dfs2(int s) {
vector<int> bef;
stack<int> ord; ord.push(s);
while (ord.size()) {
int u = ord.top(); ord.pop();
vis[u] = true, bef.push_back(u);
int v = to[u];
if (!vis[v]) { ord.push(v); continue; }
int i = bef.size() - 1;
while (true) {
cyc.push_back(bef[i]), in_cyc[bef[i]] = true;
if (bef[i] == v) break;
i--;
}
break;
}
}
lint dp[MAX_N];
lint best[MAX_N];
int par[MAX_N];
lint dfs3(int s) {
vector<int> preord;
stack<int> ord; ord.push(s);
while (ord.size()) {
int u = ord.top(); ord.pop();
preord.push_back(u);
for (pil x : from[u]) {
int v = x.first;
if (v == par[u] || in_cyc[v]) continue;
par[v] = u, ord.push(v);
}
}
for (int i = preord.size() - 1; i >= 0; i--) {
int u = preord[i];
lint max1 = 0, max2 = 0;
for (pil x : from[u]) {
int v = x.first;
if (v == par[u] || in_cyc[v]) continue;
best[u] = max(best[u], best[v]);
dp[u] = max(dp[u], dp[v] + x.second);
lint val = dp[v] + x.second;
if (val > max1) max2 = max1, max1 = val;
else if (val > max2) max2 = val;
}
best[u] = max(best[u], max1 + max2);
}
return best[s];
}
vector<lint> lens;
lint len_sum;
void precomp() {
if (cyc.size() == 2) {
set<int> lens_set;
for (int u : cyc) {
for (pil x : from[u]) {
int v = x.first;
if (!in_cyc[v]) continue;
lens_set.insert(x.second);
}
}
lens.insert(lens.begin(), lens_set.begin(), lens_set.end());
len_sum = lens[0] + lens[1];
return;
}
for (int i = 0; i < cyc.size(); i++) {
int aft = (i + 1 == (int) cyc.size()) ? 0 : i + 1;
int u = cyc[i], v = cyc[aft];
for (pil x : from[u])
if (x.first == v) lens.push_back(x.second), len_sum += x.second;
}
}
lint comp() {
multiset<lint> trans;
vector<lint> val = {0};
for (int i = 1; i < cyc.size(); i++)
val.push_back(val.back() + lens[i - 1]);
for (int i = 1; i < cyc.size(); i++)
val[i] = dp[cyc[i]] - val[i], trans.insert(val[i]);
lint shift = 0;
lint ans = 0;
for (int i = 0; i < cyc.size(); i++) {
ans = max(ans, dp[cyc[i]] + len_sum + *trans.rbegin() + shift);
if (i + 1 == (int) cyc.size()) continue;
trans.erase(trans.find(val[i + 1]));
val[i] = dp[cyc[i]] - len_sum - shift;
trans.insert(val[i]);
shift += lens[i];
}
return ans;
}
int main() {
// freopen("isl.in", "r", stdin), freopen("isl.out", "w", stdout);
cin >> n;
for (int u = 1; u <= n; u++) {
cin >> to[u];
lint len; cin >> len;
from[to[u]].push_back({u, len});
}
lint ans = 0;
for (int u = 1; u <= n; u++) {
if (seen[u]) continue;
cyc.clear(), lens.clear(), len_sum = 0;
lint new_ans = 0;
dfs1(u), dfs2(u);
for (int u : cyc) new_ans = max(new_ans, dfs3(u));
precomp();
new_ans = max(new_ans, comp());
reverse(cyc.begin(), cyc.end()), reverse(lens.begin(), next(lens.end(), -1));
new_ans = max(new_ans, comp());
ans += new_ans;
}
cout << ans << '\n';
}
Compilation message
islands.cpp: In function 'void precomp()':
islands.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int i = 0; i < cyc.size(); i++) {
| ~~^~~~~~~~~~~~
islands.cpp: In function 'lint comp()':
islands.cpp:107:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int i = 1; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
islands.cpp:109:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i = 1; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
islands.cpp:114:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int i = 0; i < cyc.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29136 KB |
Output is correct |
2 |
Correct |
5 ms |
29016 KB |
Output is correct |
3 |
Correct |
5 ms |
29020 KB |
Output is correct |
4 |
Correct |
5 ms |
29188 KB |
Output is correct |
5 |
Correct |
5 ms |
29192 KB |
Output is correct |
6 |
Correct |
4 ms |
29128 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
4 ms |
29020 KB |
Output is correct |
9 |
Correct |
5 ms |
29020 KB |
Output is correct |
10 |
Correct |
4 ms |
29020 KB |
Output is correct |
11 |
Correct |
6 ms |
29064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29212 KB |
Output is correct |
2 |
Correct |
5 ms |
29184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
29276 KB |
Output is correct |
2 |
Correct |
8 ms |
29532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30044 KB |
Output is correct |
2 |
Correct |
31 ms |
30180 KB |
Output is correct |
3 |
Correct |
24 ms |
25312 KB |
Output is correct |
4 |
Correct |
26 ms |
26292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
31472 KB |
Output is correct |
2 |
Correct |
81 ms |
32296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
41404 KB |
Output is correct |
2 |
Correct |
203 ms |
43608 KB |
Output is correct |
3 |
Correct |
262 ms |
53964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
53196 KB |
Output is correct |
2 |
Correct |
359 ms |
70428 KB |
Output is correct |
3 |
Correct |
632 ms |
75972 KB |
Output is correct |
4 |
Correct |
672 ms |
91216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
482 ms |
79212 KB |
Output is correct |
2 |
Correct |
1191 ms |
120116 KB |
Output is correct |
3 |
Correct |
528 ms |
72616 KB |
Output is correct |
4 |
Correct |
720 ms |
102424 KB |
Output is correct |
5 |
Correct |
731 ms |
102664 KB |
Output is correct |
6 |
Correct |
1054 ms |
86612 KB |
Output is correct |
7 |
Correct |
1054 ms |
131072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
540 ms |
97236 KB |
Output is correct |
2 |
Correct |
489 ms |
92520 KB |
Output is correct |
3 |
Runtime error |
742 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |