#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 s1[DIM], s2[DIM], dp[DIM], sp[DIM], mx[DIM];
vector <pair <int, int>> v[DIM];
vector <pair <int, int>> cyc;
bool onCyc[DIM];
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);
}
}
s1[nod] = mx[nod] + sp[nod];
sum = sum + elem.y;
ans = max(ans, mx[nod]);
}
for (auto elem : cyc) {
int nod = elem.x;
s2[nod] = mx[nod] + sum - sp[nod];
}
for (int i = cyc.size() - 2; i >= 0 ; --i) {
int nod = cyc[i].x;
ans = max(ans, mx[nod] - sp[nod] + s1[cyc[i + 1].x]);
ans = max(ans, mx[nod] + sp[nod] + s2[cyc[i + 1].x]);
s1[nod] = max(s1[nod], s1[cyc[i + 1].x]);
s2[nod] = max(s2[nod], s2[cyc[i + 1].x]);
}
sol += ans;
}
printf("%lld\n", sol);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
15 ms |
23936 KB |
Output is correct |
3 |
Correct |
16 ms |
23936 KB |
Output is 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 |
Correct |
15 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23808 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24064 KB |
Output is correct |
2 |
Correct |
17 ms |
24064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24064 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25336 KB |
Output is correct |
2 |
Correct |
40 ms |
28536 KB |
Output is correct |
3 |
Correct |
28 ms |
25720 KB |
Output is correct |
4 |
Correct |
21 ms |
24704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
30332 KB |
Output is correct |
2 |
Correct |
80 ms |
33524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
43220 KB |
Output is correct |
2 |
Correct |
123 ms |
45424 KB |
Output is correct |
3 |
Correct |
158 ms |
58984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
57088 KB |
Output is correct |
2 |
Correct |
262 ms |
87780 KB |
Output is correct |
3 |
Correct |
278 ms |
88044 KB |
Output is correct |
4 |
Correct |
358 ms |
122980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
72004 KB |
Output is correct |
2 |
Runtime error |
840 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
481 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |