#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL llinf=1987654321987654321;
int n;
LL ret, ans;
vector<pii> link[1000010];
int cyc[1000010], re;
LL sum[1000010];
bool ch[1000010];
inline LL get_cyc(int num, int par){
if(ch[num]){
cyc[re]=num;
sum[re++]=0;
return 0;
}
ch[num]=true;
int t=0;
for(auto i:link[num]){
if(i.F==par&&!t){
t++;
continue;
}
LL temp=get_cyc(i.F, num)+i.S;
if(temp>=0){
ch[num]=false;
cyc[re]=num;
sum[re++]=temp;
if(cyc[0]==num)return -llinf;
return temp;
}
}
ch[num]=false;
return -llinf;
}
inline LL get_maxd(int num, int par1, int par2){
LL ret2=0;
ch[num]=true;
for(auto i:link[num]){
if(i.F==par1||i.F==par2)continue;
LL temp=get_maxd(i.F, num, num)+i.S;
ret=max(ret, ret2+temp);
ret2=max(ret2, temp);
}
return ret2;
}
inline LL solve(int num){
re=0; ret=0;
get_cyc(num, 0);
LL max1=llinf, max2=-llinf, dia=sum[--re];
for(int i=0; i<re; i++){
LL tmp=get_maxd(cyc[i], cyc[(i+re-1)%re], cyc[(i+1)%re]);
ret=max(ret, sum[i]+tmp-max1);
ret=max(ret, dia-sum[i]+tmp+max2);
max2=max(max2, sum[i]+tmp);
max1=min(max1, sum[i]-tmp);
}
return ret;
}
int main(){
scanf("%d", &n);
for(int i=1; i<=n; i++){
int a, b;
scanf("%d %d", &a, &b);
link[i].pb(mp(a, b));
link[a].pb(mp(i, b));
}
for(int i=1; i<=n; i++){
if(ch[i])continue;
ans+=solve(i);
}
printf("%lld", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:74: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 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
18 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
19 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
17 ms |
23808 KB |
Output is correct |
11 |
Correct |
17 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
18 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
24032 KB |
Output is correct |
2 |
Correct |
19 ms |
24064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24448 KB |
Output is correct |
2 |
Correct |
35 ms |
26104 KB |
Output is correct |
3 |
Correct |
30 ms |
24576 KB |
Output is correct |
4 |
Correct |
22 ms |
24192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
26872 KB |
Output is correct |
2 |
Correct |
67 ms |
28640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
32448 KB |
Output is correct |
2 |
Correct |
110 ms |
35576 KB |
Output is correct |
3 |
Correct |
132 ms |
41720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
39416 KB |
Output is correct |
2 |
Correct |
222 ms |
54392 KB |
Output is correct |
3 |
Correct |
243 ms |
57848 KB |
Output is correct |
4 |
Correct |
318 ms |
69888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
402 ms |
63952 KB |
Output is correct |
2 |
Correct |
756 ms |
90104 KB |
Output is correct |
3 |
Correct |
342 ms |
54904 KB |
Output is correct |
4 |
Correct |
441 ms |
78712 KB |
Output is correct |
5 |
Correct |
433 ms |
79480 KB |
Output is correct |
6 |
Correct |
1084 ms |
60072 KB |
Output is correct |
7 |
Correct |
464 ms |
95352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
500 ms |
114392 KB |
Output is correct |
2 |
Correct |
476 ms |
94456 KB |
Output is correct |
3 |
Correct |
491 ms |
127096 KB |
Output is correct |
4 |
Correct |
443 ms |
71032 KB |
Output is correct |
5 |
Correct |
429 ms |
93688 KB |
Output is correct |
6 |
Correct |
426 ms |
78072 KB |
Output is correct |
7 |
Correct |
998 ms |
75512 KB |
Output is correct |