#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
#define MAX_N 1000000
typedef long long ll;
int N;
struct S{
int idx;
int data;
};
vector<S> gp[MAX_N+1];
int a, b;
struct C{
ll a, b;
};
deque<C> cycle;
bool vst[MAX_N+1];
int num[MAX_N+1];
ll len[MAX_N+1];
deque<int> leaf;
ll m[MAX_N+1][3];
ll ans=0;
vector<S> gp2;
int main(){
scanf("%d", &N);
for(int i=1; i<=N; i++){
scanf("%d%d", &a, &b);
gp[i].push_back((S){a, b}); num[i]++;
gp[a].push_back((S){i, b}); num[a]++;
}
leaf.resize(N); cycle.resize(N);
for(int i=1; i<=N; i++){
if(num[i]==1){
leaf.push_back(i);
}
}
while(!leaf.empty()){
int now=leaf.front(); leaf.pop_front();
vst[now]=true;
m[now][0]=max(m[now][0], m[now][1]+m[now][2]);
for(int i=0; i<gp[now].size(); i++){
if(!vst[gp[now][i].idx]){
len[gp[now][i].idx]=max(len[gp[now][i].idx], len[now]+gp[now][i].data);
m[gp[now][i].idx][0]=max(m[gp[now][i].idx][0], m[now][0]);
if(len[now]+gp[now][i].data>m[gp[now][i].idx][1]){
m[gp[now][i].idx][2]=m[gp[now][i].idx][1];
m[gp[now][i].idx][1]=len[now]+gp[now][i].data;
}else if(len[now]+gp[now][i].data>m[gp[now][i].idx][2]){
m[gp[now][i].idx][2]=len[now]+gp[now][i].data;
}
num[gp[now][i].idx]--;
if(num[gp[now][i].idx]==1){
leaf.push_back(gp[now][i].idx);
}
}
}
}
ll ans2=0;
ll sum=0, m1, m2;
int t;
for(int i=1; i<=N; i++){
if(!vst[i]){
for(int j=0; j<gp[i].size(); j++){
if(!vst[gp[i][j].idx]){
gp2.push_back(gp[i][j]);
}
}
while(!gp[i].empty()) gp[i].pop_back();
while(!gp2.empty()){
gp[i].push_back(gp2.back()); gp2.pop_back();
}
}
}
for(int i=1; i<=N; i++){
t=i;
if(!vst[i]){
cycle.push_front({gp[i][1].idx, gp[i][1].data});
while(1){
vst[t]=true;
if(vst[gp[t][0].idx]){
if(vst[gp[t][1].idx]){
break;
}
cycle.push_front({t, gp[t][1].data});
t=gp[t][1].idx;
}else{
cycle.push_front({t, gp[t][0].data});
t=gp[t][0].idx;
}
}
ans2=0; sum=0;
for(int j=0; j<cycle.size(); j++){
sum+=cycle[j].b;
ans2=max(ans2, max(m[cycle[j].a][0], m[cycle[j].a][1]+m[cycle[j].a][2]));
}
/*while(!c.empty()){
S now = c.back(); c.pop_back();
ans2=max(ans2, max(m[now.idx][0], m[now.idx][1]+m[now.idx][2]));
sum+=now.data;
cycle.push_back({now.data, len[now.idx]});
}*/
m1 = m2 = 0;
while(!cycle.empty()){
C now = cycle.back(); cycle.pop_back();
ans2=max(ans2, max(len[now.a], max(m1+len[now.a], m2+len[now.a])));
m1=max(m1+now.b, now.b+len[now.a]);
m2=max(m2-now.b, sum-now.b+len[now.a]);
}
ans+=ans2;
}
}
printf("%lld", ans);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:47:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gp[now].size(); i++){
~^~~~~~~~~~~~~~~
islands.cpp:69:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<gp[i].size(); j++){
~^~~~~~~~~~~~~
islands.cpp:98:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<cycle.size(); j++){
~^~~~~~~~~~~~~
islands.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
islands.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
4 |
Incorrect |
19 ms |
23800 KB |
Output isn't correct |
5 |
Incorrect |
19 ms |
23772 KB |
Output isn't correct |
6 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
7 |
Incorrect |
22 ms |
23800 KB |
Output isn't correct |
8 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
9 |
Incorrect |
22 ms |
23800 KB |
Output isn't correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23928 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
24056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
25336 KB |
Output is correct |
2 |
Correct |
45 ms |
27640 KB |
Output is correct |
3 |
Correct |
35 ms |
26104 KB |
Output is correct |
4 |
Correct |
29 ms |
24824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
29084 KB |
Output is correct |
2 |
Correct |
68 ms |
32900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
43896 KB |
Output is correct |
2 |
Correct |
128 ms |
41044 KB |
Output is correct |
3 |
Correct |
162 ms |
49784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
59228 KB |
Output is correct |
2 |
Correct |
270 ms |
73136 KB |
Output is correct |
3 |
Correct |
277 ms |
63220 KB |
Output is correct |
4 |
Correct |
335 ms |
87888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
116544 KB |
Output is correct |
2 |
Correct |
1034 ms |
119520 KB |
Output is correct |
3 |
Correct |
445 ms |
98040 KB |
Output is correct |
4 |
Correct |
513 ms |
120056 KB |
Output is correct |
5 |
Correct |
493 ms |
121040 KB |
Output is correct |
6 |
Correct |
1324 ms |
115320 KB |
Output is correct |
7 |
Correct |
530 ms |
130408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
463 ms |
112120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |