# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
125127 |
2019-07-04T16:31:00 Z |
NMAC427 |
Islands (IOI08_islands) |
C++17 |
|
857 ms |
131076 KB |
// https://oj.uz/problem/view/IOI08_islands
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
using ll = long long int;
int N;
vector<pair<int, int>> to;
vector<vector<int>> from;
vector<bool> inCycle;
pair<ll, ll> dfsSolve(int v) {
ll max1 = 0;
ll max2 = 0;
ll maxInternal = 0; // Maximum internal path;
for (const int child: from[v]) {
if (inCycle[child]) continue; // Check here because we want to be able to pass in a node of the cycle
auto childDFS = dfsSolve(child);
childDFS.ff += to[child].ss;
if (childDFS.ff >= max1) {
max2 = max1;
max1 = childDFS.ff;
} else if (childDFS.ff > max2) {
max2 = childDFS.ff;
}
maxInternal = max(maxInternal, childDFS.ss);
}
maxInternal = max(maxInternal, max1 + max2);
return {max1, maxInternal};
}
signed main() {
#ifndef __APPLE__
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> N;
to.resize(N);
from.resize(N);
for (int i = 0; i < N; i++) {
cin >> to[i].ff >> to[i].ss;
to[i].ff -= 1;
from[to[i].ff].push_back(i);
}
// Generate inCycle Vector
inCycle.assign(N, false);
vector<bool> visited (N, false);
for (int i = 0; i < N; i++) {
if (visited[i]) continue;
vector<int> cycle;
int next = i;
while (true) {
visited[next] = true;
cycle.push_back(next);
if (visited[to[next].ff]) {
bool __inCycle = false;
for (int v: cycle) {
if (to[next].ff == v) __inCycle = true;
if (__inCycle) inCycle[v] = true;
}
break;
} else {
next = to[next].ff;
}
}
}
visited.clear();
// Solve
ll ans = 0;
for (int i = 0; i < N; i++) {
if (!inCycle[i]) continue;
ll compAns = 0;
vector<int> cycle;
vector<ll> dfsV;
vector<ll> prefixSum = {0};
int next = i;
do {
auto dfs = dfsSolve(next);
compAns = max(compAns, dfs.ss); // Internal Path
cycle.push_back(next);
dfsV.push_back(dfs.ff);
prefixSum.push_back(prefixSum.back() + to[next].ss);
next = to[next].ff;
} while (next != i);
for (int v: cycle) {
inCycle[v] = false; // Don't visit same component twice
}
ll prefixFwd = dfsV[0];
ll prefixBwd = dfsV[0];
for(int i = 1; i < cycle.size(); i++) {
compAns = max(compAns, dfsV[i] + prefixSum[i] + prefixFwd);
compAns = max(compAns, dfsV[i] - prefixSum[i] + prefixBwd + prefixSum.back());
prefixFwd = max(prefixFwd, dfsV[i] - prefixSum[i]);
prefixBwd = max(prefixBwd, dfsV[i] + prefixSum[i]);
}
ans += compAns;
}
cout << ans << "\n";
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:123:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cycle.size(); i++) {
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1400 KB |
Output is correct |
2 |
Correct |
18 ms |
2936 KB |
Output is correct |
3 |
Correct |
12 ms |
1784 KB |
Output is correct |
4 |
Correct |
7 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
3620 KB |
Output is correct |
2 |
Correct |
41 ms |
6912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
11180 KB |
Output is correct |
2 |
Correct |
91 ms |
18136 KB |
Output is correct |
3 |
Correct |
113 ms |
19448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
22536 KB |
Output is correct |
2 |
Correct |
209 ms |
30624 KB |
Output is correct |
3 |
Correct |
216 ms |
46440 KB |
Output is correct |
4 |
Correct |
277 ms |
45548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
341 ms |
36548 KB |
Output is correct |
2 |
Correct |
618 ms |
60900 KB |
Output is correct |
3 |
Correct |
323 ms |
41456 KB |
Output is correct |
4 |
Correct |
415 ms |
62768 KB |
Output is correct |
5 |
Correct |
416 ms |
60364 KB |
Output is correct |
6 |
Correct |
857 ms |
50532 KB |
Output is correct |
7 |
Correct |
432 ms |
75576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
415 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |