#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortvec(x) sort(all(x))
#define compress(x) x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=1987654321987654321;
const int inf=2000000000;
int n;
vector<pil> link[1000010];
bool ch[1000010];
LL ret, ans;
vector<int> cyc;
vector<LL> sum;
LL get_cyc(int num, int par){
if(ch[num]){
cyc.pb(num);
sum.pb(0);
return 0;
}
LL retnum=-llinf;
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){
cyc.pb(num);
sum.pb(temp);
if(cyc[0]!=num)retnum=temp;
}
}
return retnum;
}
LL get_maxd(int num, int par1, int par2){
LL ret2=0;
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;
}
LL solve(int num){
cyc.clear(); sum.clear();
ret=0;
get_cyc(num, 0);
LL max1=llinf, max2=-llinf, dia=sum.back();
cyc.pop_back();
sum.pop_back();
int sz=cyc.size();
for(int i=0; i<sz; i++){
LL tmp=get_maxd(cyc[i], cyc[(i+sz-1)%sz], cyc[(i+1)%sz]);
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; LL b;
scanf("%d %lld", &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:78:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %lld", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
78 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Runtime error |
103 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Runtime error |
87 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Incorrect |
19 ms |
23808 KB |
Output isn't correct |
5 |
Runtime error |
78 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
6 |
Runtime error |
74 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
7 |
Runtime error |
76 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Runtime error |
119 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Runtime error |
77 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
10 |
Incorrect |
18 ms |
23808 KB |
Output isn't correct |
11 |
Incorrect |
18 ms |
23808 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
94 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
84 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
222 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
138 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
342 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
214 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
393 ms |
71740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
462 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |