//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo n;
vector<pair<int,int>> adj[1000001];
int par[1000001];
int find(int no){
if(par[no]==no){
return no;
}
par[no]=find(par[no]);
return par[no];
}
llo dp[1000001][2];
pair<int,int> cur;
vector<pair<int,int>> pp;
//vector<pair<int,int>> xx;
//bool vis[1000001];
//vector<int> tt;
int stt=0;
void dfs(int no,int par2=-1){
if(no==cur.b){
//pp=xx;
stt=1;
}
//tt.pb(no);
for(auto j:adj[no]){
if(j.a!=par2){
if(stt==0){
pp.pb({j.a,j.b});
}
dfs(j.a,no);
if(stt==0){
pp.pop_back();
}
}
}
}
llo maa=0;
void dfs2(int no,int par2=-1){
dp[no][0]=0;
dp[no][1]=0;
llo ma=0;
llo ma2=0;
for(auto j:adj[no]){
if(par[j.a]){
continue;
}
if(j.a!=par2){
dfs2(j.a,no);
if(dp[j.a][0]+j.b>=ma){
ma2=ma;
ma=dp[j.a][0]+j.b;
}
else if(dp[j.a][0]+j.b>=ma2){
ma2=dp[j.a][0]+j.b;
}
}
}
dp[no][1]=ma+ma2;
dp[no][0]=ma;
maa=max(maa,dp[no][1]);
}
llo ans=0;
multiset<llo> zz;
vector<pair<pair<int,int>,int>> ed;
int x,y;
llo aa,cc;
int xx;
llo su2,su3;
llo cost=0;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(llo i=0;i<n;i++){
par[i]=i;
}
for(llo i=0;i<n;i++){
cin>>aa>>cc;
aa--;
x=find(i);
y=find(aa);
if(x!=y){
par[x]=y;
adj[i].pb({aa,cc});
adj[aa].pb({i,cc});
}
else{
ed.pb({{i,aa},cc});
}
}
for(int i=0;i<n;i++){
par[i]=0;
}
for(auto j:ed){
cur=j.a;
cost=0;
pp.clear();
stt=0;
dfs(j.a.a);
pp.pb({cur.a,j.b});
for(auto i:pp){
par[i.a]=1;
}
maa=0;
for(auto i:pp){
dfs2(i.a);
}
cost=max(cost,maa);
xx=pp.size();
for(int i=0;i<xx;i++){
pp.pb(pp[i]);
}
su2=0;
su3=0;
for(llo i=0;i<pp.size();i++){
if(i>=xx){
su3+=pp[i-xx].b;
auto jj=zz.find(dp[pp[i-xx].a][0]-su3);
zz.erase(jj);
}
su2+=pp[i].b;
if(zz.size()){
auto jj=zz.end();
jj--;
cost=max(cost,dp[pp[i].a][0]+su2+(*jj));
}
if(i<xx){
zz.insert(dp[pp[i].a][0]-su2);
}
}
ans+=cost;
}
cout<<ans<<endl;
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:138:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(llo i=0;i<pp.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
18 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
17 ms |
23788 KB |
Output is correct |
7 |
Correct |
16 ms |
23916 KB |
Output is correct |
8 |
Correct |
16 ms |
23788 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23776 KB |
Output is correct |
11 |
Correct |
17 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
18 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
25068 KB |
Output is correct |
2 |
Correct |
39 ms |
28008 KB |
Output is correct |
3 |
Correct |
28 ms |
25068 KB |
Output is correct |
4 |
Correct |
24 ms |
24428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
29544 KB |
Output is correct |
2 |
Correct |
59 ms |
31936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
37860 KB |
Output is correct |
2 |
Correct |
130 ms |
46304 KB |
Output is correct |
3 |
Correct |
162 ms |
59356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
48232 KB |
Output is correct |
2 |
Correct |
248 ms |
80984 KB |
Output is correct |
3 |
Correct |
361 ms |
91220 KB |
Output is correct |
4 |
Correct |
378 ms |
114888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
82632 KB |
Output is correct |
2 |
Runtime error |
838 ms |
131076 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
394 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |