#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
void usaco(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
typedef long long ll;
#define USING_ORDERED_SET 0
#if USING_ORDERED_SET
#include<bits/extc++.h>
using namespace __gnu_pbds;
template<class T>using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#endif
template<class T>void umax(T &a,T b){if(a<b)a=b;}
template<class T>void umin(T &a,T b){if(b<a)a=b;}
#ifdef IOI2021SG
#define printl(args...)printf(args)
#else
#define printl(args...)((void)0)
#endif
const int N=1e6+5;
vector<pair<int,int>>g[N];
vector<pair<int,int>>gr[N];
vector<pair<int,int>>g1[N];
vector<pair<int,int>>g2[N];
int n;
pair<int,int>a[N];
int tin[N];
int fup[N];
bool used[N];
int timer;
vector<pair<int,int>>bridges;
void bridge(int v,int p){
used[v]=true;
fup[v]=tin[v]=++timer;
for(auto [to,w]:gr[v])if(to!=p)
if(used[to])umin(fup[v],tin[to]);
else{
bridge(to,v);
umin(fup[v],fup[to]);
if(fup[to]>tin[v]&&a[v].fr!=to){
bridges.emplace_back(v,to);
bridges.emplace_back(to,v);
g1[v].emplace_back(to,w);
g1[to].emplace_back(v,w);
}
}
}
vector<int>container;
int weight[2*N];
void cycle(int v){
used[v]=true;
container.push_back(v);
for(auto [to,w]:g2[v])if(!used[to])cycle(to);
}
ll mx_depth[N];
void depth(int v,int p=0){
for(auto [to,w]:g1[v])if(to!=p){
depth(to,v);
umax(mx_depth[v],mx_depth[to]+1ll*w);
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].fr,&a[i].sc);
g[i].emplace_back(a[i].fr,a[i].sc);
g[a[i].fr].emplace_back(i,a[i].sc);
if(a[a[i].fr].fr!=i){
gr[i].emplace_back(a[i].fr,a[i].sc);
gr[a[i].fr].emplace_back(i,a[i].sc);
}
}
for(int i=1;i<=n;i++)if(!used[i])bridge(i,i);
sort(bridges.begin(),bridges.end());
for(int i=1;i<=n;i++){
if(!binary_search(bridges.begin(),bridges.end(),make_pair(i,a[i].fr))){
g2[i].emplace_back(a[i].fr,a[i].sc);
g2[a[i].fr].emplace_back(i,a[i].sc);
}
used[i]=0;
}
ll ans=0;
for(int i=1;i<=n;i++)if(!used[i]){
container.clear();
cycle(i);
if(container.size()>1){
int sz=container.size();
for(int i=0;i<sz;i++){
container.push_back(container[i]);
depth(container[i]);
}
sz<<=1;
for(int i=0;i+1<sz;i++){
if(a[container[i]].fr==container[i+1])weight[i]=a[container[i]].sc;
else{
weight[i]=a[container[i+1]].sc;
}
}
int csz=(sz>>1);
ll mx=0;
for(int i=0;i<sz;i++){
ll tmp=0;
for(int j=i+1;j<sz;j++){
if(j-i>=csz)continue;
tmp+=1ll*weight[j-1];
umax(mx,tmp+1ll*mx_depth[container[i]]+1ll*mx_depth[container[j]]);
}
}
reverse(container.begin(),container.end());
for(int i=0;i+1<sz;i++){
if(a[container[i]].fr==container[i+1])weight[i]=a[container[i]].sc;
else{
weight[i]=a[container[i+1]].sc;
}
}
for(int i=0;i<sz;i++){
ll tmp=0;
for(int j=i+1;j<sz;j++){
if(j-i>=csz)continue;
tmp+=1ll*weight[j-1];
umax(mx,tmp+1ll*mx_depth[container[i]]+1ll*mx_depth[container[j]]);
}
}
ans+=mx;
}
}
printf("%lld",ans);
}
Compilation message
islands.cpp: In function 'void bridge(int, int)':
islands.cpp:35:29: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
35 | for(auto [to,w]:gr[v])if(to!=p)
| ^
islands.cpp: In function 'void usaco(std::string)':
islands.cpp:5:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | void usaco(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp:5:66: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | void usaco(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
islands.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d",&a[i].fr,&a[i].sc);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
94148 KB |
Output is correct |
2 |
Incorrect |
63 ms |
94312 KB |
Output isn't correct |
3 |
Correct |
65 ms |
94268 KB |
Output is correct |
4 |
Incorrect |
75 ms |
94172 KB |
Output isn't correct |
5 |
Correct |
68 ms |
94148 KB |
Output is correct |
6 |
Incorrect |
64 ms |
94172 KB |
Output isn't correct |
7 |
Incorrect |
70 ms |
94264 KB |
Output isn't correct |
8 |
Incorrect |
63 ms |
94148 KB |
Output isn't correct |
9 |
Incorrect |
66 ms |
94148 KB |
Output isn't correct |
10 |
Incorrect |
64 ms |
94244 KB |
Output isn't correct |
11 |
Correct |
64 ms |
94224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
94388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
94484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
176 ms |
96676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2071 ms |
104604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2067 ms |
126984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
259 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
251 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
362 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |