//#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'
int n;
vector<pair<int,int>> adj[1000001];
vector<pair<int,int>> adj2[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<llo,llo> cur;
vector<pair<llo,llo>> pp;
vector<pair<llo,llo>> xx;
vector<llo> tt;
void dfs(int no,int par2=-1){
if(no==cur.b){
pp=xx;
}
tt.pb(no);
for(auto j:adj[no]){
/*if(j.a==cur.a and no==cur.b){
continue;
}
if(j.a==cur.b and no==cur.a){
continue;
}*/
if(j.a!=par2){
xx.pb({j.a,j.b});
dfs(j.a,no);
xx.pop_back();
}
}
}
int vis[1000001];
void dfs2(int no,int par2=-1){
dp[no][0]=0;
dp[no][1]=0;
vector<llo> ss;
llo su=0;
for(auto j:adj2[no]){
if(j.a!=par2){
dfs2(j.a,no);
// dp[no][0]=max(dp[no][0],j.b+dp[j.a][0]);
su+=dp[j.a][1];
ss.pb(dp[j.a][0]+j.b-dp[j.a][1]);
}
}
sort(ss.begin(),ss.end());
reverse(ss.begin(),ss.end());
dp[no][1]=su;
dp[no][0]=su;
for(int i=0;i<ss.size();i++){
if(i==2){
break;
}
if(i==0){
if(ss[i]>=0){
dp[no][0]+=ss[i];
}
}
if(ss[i]>=0){
dp[no][1]+=ss[i];
}
}
dp[no][1]=max(dp[no][1],dp[no][0]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=0;i<n;i++){
par[i]=i;
}
vector<pair<pair<int,int>,int>> ed;
for(int i=0;i<n;i++){
int aa,cc;
cin>>aa>>cc;
aa--;
int x=find(i);
int 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});
//cout<<i<<":"<<aa<<":"<<cc<<endl;
}
}
llo ans=0;
for(auto j:ed){
cur=j.a;
llo cost=0;
tt.clear();
dfs(j.a.a);
pp.pb({cur.a,j.b});
for(auto i:pp){
vis[i.a]=1;
}
for(auto i:tt){
for(auto k:adj[i]){
if(vis[k.a]+vis[i]<2){
adj2[i].pb(k);
}
}
}
/*for(auto i:tt){
cout<<i<<",";
}
cout<<endl;*/
llo kk=0;
for(auto i:pp){
dfs2(i.a);
kk+=dp[i.a][1];
//cout<<i.a<<":"<<1<<endl;
}
cost=max(cost,kk);
int xx=pp.size();
for(int i=0;i<pp.size();i++){
int ind=i;
llo su=0;
for(int cot=0;cot<xx-1;cot++){
ind++;
if(ind==xx){
ind=0;
}
su+=pp[ind].b;
llo cur2=su;
cur2+=dp[pp[i].a][0]-dp[pp[i].a][1];
cur2+=dp[pp[ind].a][0]-dp[pp[ind].a][1];
cost=max(cost,cur2+kk);
su-=(dp[pp[ind].a][1]);
}
}
for(auto i:pp){
vis[i.a]=0;
}
ans+=cost;
}
cout<<ans<<endl;
return 0;
}
Compilation message
islands.cpp: In function 'void dfs2(int, int)':
islands.cpp:71:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i=0;i<ss.size();i++){
| ~^~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:147:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for(int i=0;i<pp.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
47468 KB |
Output is correct |
2 |
Incorrect |
35 ms |
47340 KB |
Output isn't correct |
3 |
Correct |
41 ms |
47492 KB |
Output is correct |
4 |
Correct |
35 ms |
47340 KB |
Output is correct |
5 |
Correct |
39 ms |
47340 KB |
Output is correct |
6 |
Correct |
36 ms |
47496 KB |
Output is correct |
7 |
Correct |
37 ms |
47336 KB |
Output is correct |
8 |
Incorrect |
33 ms |
47340 KB |
Output isn't correct |
9 |
Correct |
35 ms |
47364 KB |
Output is correct |
10 |
Correct |
37 ms |
47376 KB |
Output is correct |
11 |
Correct |
35 ms |
47340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
47616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
47468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
82 ms |
49184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2029 ms |
55460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2017 ms |
72940 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2068 ms |
76668 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
499 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
502 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |