#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int DIM = 1e6 + 1;
int n;
long long ans;
bool viz[DIM];
int tt[DIM], ct[DIM];
long long s[DIM], dp[DIM], sp[DIM], mx[DIM];
vector <pair <int, int>> v[DIM];
int nr;
int cyc[DIM];
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[++nr] = now;
else cyc[++nr] = now, ct[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;
int nod;
for (int i = 1; i <= n ; ++i) {
if (viz[i]) continue ;
nr = 0; ans = 0;
find_cycle(i);
long long sum = 0;
for (int i = 1; i <= nr ; ++i) {
nod = cyc[i];
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);
}
}
s[nod] = mx[nod] + sp[nod];
sum = sum + ct[nod];
ans = max(ans, mx[nod]);
}
for (int i = nr - 1; i >= 1 ; --i) {
nod = cyc[i];
ans = max(ans, mx[nod] - sp[nod] + s[cyc[i + 1]]);
s[nod] = max(s[nod], s[cyc[i + 1]]);
}
for (int i = 1; i <= nr ; ++i) {
nod = cyc[i];
s[nod] = mx[nod] + sum - sp[nod];
}
for (int i = nr - 1; i >= 1 ; --i) {
nod = cyc[i];
ans = max(ans, mx[nod] + sp[nod] + s[cyc[i + 1]]);
s[nod] = max(s[nod], s[cyc[i + 1]]);
}
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 |
23808 KB |
Output is correct |
3 |
Correct |
15 ms |
23936 KB |
Output is correct |
4 |
Correct |
16 ms |
23936 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 |
23936 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
15 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23936 KB |
Output is correct |
2 |
Correct |
16 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24064 KB |
Output is correct |
2 |
Correct |
17 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25216 KB |
Output is correct |
2 |
Correct |
43 ms |
27740 KB |
Output is correct |
3 |
Correct |
28 ms |
25464 KB |
Output is correct |
4 |
Correct |
26 ms |
24704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
29048 KB |
Output is correct |
2 |
Correct |
70 ms |
31920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
40804 KB |
Output is correct |
2 |
Correct |
124 ms |
41464 KB |
Output is correct |
3 |
Correct |
156 ms |
51704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
53368 KB |
Output is correct |
2 |
Correct |
248 ms |
76660 KB |
Output is correct |
3 |
Correct |
276 ms |
74744 KB |
Output is correct |
4 |
Correct |
356 ms |
94712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
71888 KB |
Output is correct |
2 |
Correct |
939 ms |
115704 KB |
Output is correct |
3 |
Correct |
380 ms |
84984 KB |
Output is correct |
4 |
Correct |
492 ms |
118040 KB |
Output is correct |
5 |
Correct |
473 ms |
122744 KB |
Output is correct |
6 |
Correct |
1241 ms |
81548 KB |
Output is correct |
7 |
Runtime error |
529 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
522 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |