# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
217695 | mhy908 | Islands (IOI08_islands) | C++14 | 1093 ms | 131072 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long LL;
typedef pair<int, int> pii;
const LL llinf=1987654321987654321;
int n;
LL ret, ans;
vector<pii> link[1000010];
int cyc[900010], re;
LL sum[900010];
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;
bool t=true;
for(pii i:link[num]){
if(i.F==par&&t){
t=false;
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(pii 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;
}
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 j=1; j<=n; j++){
if(ch[j])continue;
re=0; ret=0;
get_cyc(j, 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);
}
ans+=ret;
}
printf("%lld", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |