#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];
set <pair <long long, int>> s1;
set <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] + 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);
mx[nod] = max(mx[nod], dp[it.x] + it.y);
}
}
s1.insert({mx[nod] + sp[nod], nod});
sum = sum + elem.y;
ans = max(ans, mx[nod]);
}
for (auto elem : cyc) {
int nod = elem.x;
s2.insert({mx[nod] + sum - sp[nod], nod});
}
for (auto elem : cyc) {
int nod = elem.x;
s1.erase({mx[nod] + sp[nod], nod});
s2.erase({mx[nod] + sum - sp[nod], nod});
if (s1.empty()) break ;
ans = max(ans, mx[nod] - sp[nod] + s1.rbegin()->x);
ans = max(ans, mx[nod] + sp[nod] + s2.rbegin()->x);
}
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);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
15 ms |
23808 KB |
Output isn't 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 |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 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 |
Incorrect |
15 ms |
23808 KB |
Output isn't correct |
11 |
Correct |
15 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24064 KB |
Output is correct |
2 |
Correct |
16 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23936 KB |
Output is correct |
2 |
Correct |
20 ms |
24576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
25336 KB |
Output is correct |
2 |
Correct |
47 ms |
29816 KB |
Output is correct |
3 |
Correct |
28 ms |
25336 KB |
Output is correct |
4 |
Incorrect |
21 ms |
24576 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
31860 KB |
Output is correct |
2 |
Correct |
93 ms |
34340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
42360 KB |
Output is correct |
2 |
Correct |
213 ms |
52332 KB |
Output is correct |
3 |
Correct |
272 ms |
73828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
52600 KB |
Output is correct |
2 |
Correct |
394 ms |
104932 KB |
Output is correct |
3 |
Correct |
671 ms |
112360 KB |
Output is correct |
4 |
Runtime error |
464 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
454 ms |
71760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
495 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |