# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
312169 |
2020-10-12T14:35:16 Z |
Akashi |
Islands (IOI08_islands) |
C++14 |
|
922 ms |
131076 KB |
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int DIM = 1e6 + 5;
int n;
long long ans;
bool viz[DIM];
int tt[DIM], ct[DIM];
long long dp[DIM], sp[DIM], mx[DIM];
vector <pair <int, int>> v[DIM];
vector <pair <int, int>> cyc;
bool onCyc[DIM];
stack <pair <long long, int>> s1;
stack <pair <long long, int>> s2;
bool find_cycle(int nod) {
viz[nod] = true;
bool found = false;
bool foundPapa = false;
for (auto it : v[nod]) {
if (it.x == tt[nod] && !foundPapa) {
foundPapa = true;
continue ;
}
if (!found && viz[it.x]) {
int now = nod;
while (now != tt[it.x]) {
if (now != it.x) cyc.push_back({now, ct[now]});
else cyc.push_back({now, it.y});
onCyc[now] = true;
now = tt[now];
}
found = true;
continue ;
}
if (viz[it.x]) continue ;
ct[it.x] = it.y; tt[it.x] = nod;
bool ok = find_cycle(it.x);
found |= ok;
}
return found;
}
void dfs(int nod, int papa = 0) {
for (auto it : v[nod]) {
if (onCyc[it.x] || it.x == papa) continue ;
dfs(it.x, nod);
ans = max(ans, dp[nod] + dp[it.x] + it.y);
dp[nod] = max(dp[nod], dp[it.x] + it.y);
}
}
int main() {
scanf("%d", &n);
int x, y;
for (int i = 1; i <= n ; ++i) {
scanf("%d%d", &x, &y);
v[i].push_back({x, y});
v[x].push_back({i, y});
}
long long sol = 0;
for (int i = 1; i <= n ; ++i) {
if (viz[i]) continue ;
cyc.clear();
ans = 0;
find_cycle(i);
long long sum = 0;
for (auto elem : cyc) {
int nod = elem.x;
sp[nod] = sum;
for (auto it : v[nod]) {
if (!onCyc[it.x]) {
dfs(it.x);
ans = max(ans, mx[nod] + dp[it.x] + it.y);
mx[nod] = max(mx[nod], dp[it.x] + it.y);
}
}
while (!s1.empty() && mx[nod] + sp[nod] > s1.top().x) s1.pop();
s1.push({mx[nod] + sp[nod], nod});
sum = sum + elem.y;
ans = max(ans, mx[nod]);
}
for (auto elem : cyc) {
int nod = elem.x;
while (!s2.empty() && mx[nod] + sum - sp[nod] > s2.top().x) s2.pop();
s2.push({mx[nod] + sum - sp[nod], nod});
}
for (auto elem : cyc) {
int nod = elem.x;
if (!s1.empty() && s1.top().y == nod) s1.pop();
if (!s2.empty() && s2.top().y == nod) s2.pop();
if (s1.empty()) break ;
ans = max(ans, mx[nod] - sp[nod] + s1.top().x);
ans = max(ans, mx[nod] + sp[nod] + s2.top().x);
}
while (!s1.empty()) s1.pop();
while (!s2.empty()) s2.pop();
sol += ans;
}
printf("%lld\n", sol);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
15 ms |
23936 KB |
Output isn't correct |
3 |
Incorrect |
15 ms |
23936 KB |
Output isn't correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
15 ms |
23808 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Incorrect |
15 ms |
23808 KB |
Output isn't correct |
8 |
Incorrect |
15 ms |
23808 KB |
Output isn't correct |
9 |
Incorrect |
17 ms |
23808 KB |
Output isn't correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
25208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
29556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
120 ms |
40052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
213 ms |
51192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
445 ms |
71760 KB |
Output is correct |
2 |
Runtime error |
922 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
503 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |