#include <bits/stdc++.h>
#define f first
#define s second.first
#define tr second.second
#define mp make_pair
using namespace std;
//ofstream out("output.txt");
const int MAXN =1e6+1;
typedef long long ll;
int read(){
int r=0;
char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
r=ch-'0';
ch=getchar();
while(ch>='0'&&ch<='9'){
r=r*10+ch-'0';
ch=getchar();
}
return r;
}
vector<pair<int,pair<ll,int> > > grafo[MAXN];
map<int,ll> m[4];
deque<int> cyc;
map<int,ll> succ;
bitset<MAXN> v;
bitset<MAXN> k;
map<int,ll> zipp;
int pos[MAXN];
bool find_cyc(int n, int pa){
v[n]=1;
k[n]=1;
cyc.push_back(n);
for(auto x:grafo[n]){
if(!v[x.f]){
if(find_cyc(x.f,x.tr))return true;
}
else if(x.tr!=pa){
cyc.push_back(x.f);
return true;
}
}
cyc.pop_back();
return false;
}
ll zip(int n, int a, int b, int c){
k[n]=1;
ll mas=0;
for(auto x:grafo[n]){
if(x.f==a||x.f==b||x.f==c)continue;
mas=max(mas,zip(x.f,n,b,c)+x.s);
}
return mas;
}
ll dfs(int n, int a){
ll mas=0;
pos[n]=-1;
k[n]=1;
for(auto x:grafo[n]){
if(n==cyc.front()&&x.f==cyc.back())continue;
if(x.f==cyc.front()&&n==cyc.back())continue;
if(x.f==a)continue;
ll p=dfs(x.f,n)+x.s;
if(mas<p){
mas=p;
pos[n]=pos[x.f];
}
}
if(pos[n]==-1)pos[n]=n;
return mas;
}
ll dfs2(int n, int a, int w){
ll mas=0;
pos[n]=-1;
k[n]=1;
for(auto x:grafo[n]){
if(n==cyc.front()&&x.f==cyc.back()&&x.s!=w)continue;
if(x.f==cyc.front()&&n==cyc.back()&&x.s!=w)continue;
if(x.f==a)continue;
ll p=dfs2(x.f,n,w)+x.s;
if(mas<p){
mas=p;
pos[n]=pos[x.f];
}
}
if(pos[n]==-1)pos[n]=n;
return mas;
}
int N;
ll process(int t){
cyc.clear();
find_cyc(t,-1);
while(cyc.front()!=cyc.back())cyc.pop_front();
cyc.pop_front();
zipp[cyc[0]]=zip(cyc[0],-1,cyc[1],cyc.back());
for(int i=1;i<cyc.size()-1;i++)zipp[cyc[i]]=zip(cyc[i],-1,cyc[i-1],cyc[i+1]);
zipp[cyc.back()]=zip(cyc.back(),-1,cyc[cyc.size()-2],cyc.front());
if(cyc.size()==2){
ll mass=0;
for(auto x:grafo[cyc[0]])
if(x.f==cyc[1]){
mass=max(mass,x.s);
}
dfs2(t,-1,mass);
return dfs2(pos[t],-1,mass);
}
for(int i=1;i<cyc.size()-1;i++)
for(auto x:grafo[cyc[i]])
if(x.f==cyc[i+1]){
succ[cyc[i]]=x.s;
break;
}
ll speciale=0;
for(auto x:grafo[cyc[0]]){
if(x.f==cyc[1])succ[cyc[0]]=x.s;
if(x.f==cyc.back())speciale=x.s;
}
int se=cyc.size();
m[0][cyc[0]]=0;
for(int i=1;i<se;i++)m[0][cyc[i]]=m[0][cyc[i-1]]+succ[cyc[i-1]];
m[1][cyc[0]]=zipp[cyc[0]];
for(int i=1;i<se;i++)m[1][cyc[i]]=max(m[1][cyc[i-1]],m[0][cyc[i]]+zipp[cyc[i]]);
m[2][cyc.back()]=0;
for(int i=se-2;i>=0;i--)m[2][cyc[i]]=m[2][cyc[i+1]]+succ[cyc[i]];
m[3][cyc.back()]=zipp[cyc.back()];
for(int i=se-2;i>=0;i--)m[3][cyc[i]]=max(m[3][cyc[i+1]],m[2][cyc[i]]+zipp[cyc[i]]);
ll ans=0;
for(int i=0;i<se-1;i++)ans=max(ans,m[1][cyc[i]]+m[3][cyc[i+1]]+speciale);
dfs(t,-1);
ans=max(ans,dfs(pos[t],-1));
return ans;
}
int main(){
//freopen("input.txt","r",stdin);
N=read();
for(int i=0;i<N;i++){
int a,c;
a=read(),c=read();
a--;
grafo[a].push_back(mp(i,mp((ll)c,i)));
grafo[i].push_back(mp(a,mp((ll)c,i)));
}
ll ans=0;
for(int i=0;i<N;i++)
if(!k[i])ans+=process(i);
cout<<ans;
}
Compilation message
islands.cpp: In function 'll process(int)':
islands.cpp:102:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<cyc.size()-1;i++)zipp[cyc[i]]=zip(cyc[i],-1,cyc[i-1],cyc[i+1]);
~^~~~~~~~~~~~~
islands.cpp:113:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<cyc.size()-1;i++)
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23800 KB |
Output is correct |
3 |
Correct |
21 ms |
24056 KB |
Output is correct |
4 |
Correct |
20 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
20 ms |
23800 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
24056 KB |
Output is correct |
2 |
Correct |
21 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
24440 KB |
Output is correct |
2 |
Correct |
27 ms |
25080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
27768 KB |
Output is correct |
2 |
Correct |
105 ms |
33876 KB |
Output is correct |
3 |
Correct |
33 ms |
25720 KB |
Output is correct |
4 |
Correct |
25 ms |
24824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
37496 KB |
Output is correct |
2 |
Correct |
196 ms |
55800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
48504 KB |
Output is correct |
2 |
Correct |
644 ms |
102684 KB |
Output is correct |
3 |
Correct |
659 ms |
114680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
621 ms |
122848 KB |
Output is correct |
2 |
Runtime error |
533 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
84256 KB |
Output is correct |
2 |
Runtime error |
650 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
375 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |