#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(Ofast)
#pragma GCC optimization (unroll-loops)
#pragma GCC target(avx,avx2,fma)
#pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native)
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#define name "main"
typedef long long ll;
typedef pair<int,int> ii;
#define fi first
#define se second
const int N = (int)1e6;
vector<ii> ke[N+2];
int cost[N+2],gi[N+2],vis[N+2],n,cha[N+2],arr[N+2],sz=0,times=0;
ll dep[N*2+2]={},pre[N*2+2]={},f[N*2+2]={};
bool com[N+2];
void add_edge(int u , int v , int w){
ke[u].push_back({v,w});
ke[v].push_back({u,w});
}
void search(int u , int p){
vis[u] = ++times;
for (auto& x:ke[u]){
int v=x.fi,w=x.se;
if (v==p) continue;
if (!vis[v]) {
cha[v]=u;
gi[v]=w;
search(v,u);
}
else{
if (vis[v]<vis[u]) continue;
++sz;
arr[sz]=v;cost[sz]=w;
for (; v!=u; v=cha[v]){
++sz;
arr[sz]=cha[v];
cost[sz]=gi[v];
}
}
}
return ;
}
// the component has at most 1 cycle inside ~
ll mx,ans=0;
int pos,root;
void dfs(int u , int p , ll s){
if (mx < s){
mx = s;
pos = u;
}
for (auto& x:ke[u]){
int v=x.fi,w=x.se;
if ((com[v]&&v!=root)||v==p) continue;
dfs(v,u,s+w);
}
return;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1; i<=n; ++i){
int en,l; cin>>en>>l;
ke[i].push_back({en,l});
ke[en].push_back({i,l});
}
for (int id=1; id<=n; ++id){
if (!vis[id]){
sz = times = 0;
search(id,id);
ll addmore = -1;
for (int i = 1; i <= sz; ++i) com[arr[i]] = true;
for (int i = 1; i <= sz; ++i){
mx=pos=root=-1;
dfs(arr[i],0,0);
dep[i]=mx;
root=arr[i];
dfs(pos,0,0);
addmore = max(addmore,mx);
pre[i]=pre[i-1]+cost[i];
}
for (int i = sz + 1; i <= sz*2; ++i){
cost[i] = cost[i-sz];
dep[i] = dep[i-sz];
pre[i]=pre[i-1]+cost[i];
}
for (int i=1; i<=sz*2; ++i) f[i]=dep[i]-pre[i];
deque<int> q;
for (int i = 1; i <= sz*2; ++i){
while (q.size() && q.front()<i-sz+1) q.pop_front();
if (q.size()){
addmore = max(addmore,pre[i]+dep[i]+f[q.front()]);
}
while (q.size() && f[q.back()]<=f[i]) q.pop_back();
q.push_back(i);
}
ans+=addmore;
}
}
cout<<ans;
exit(0);
}
Compilation message
islands.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization (unroll-loops)
|
islands.cpp:3:23: warning: '#pragma GCC optimize' is not a string or number [-Wpragmas]
3 | #pragma GCC optimize(Ofast)
| ^~~~~
islands.cpp:5:24: warning: '#pragma GCC option' is not a string [-Wpragmas]
5 | #pragma GCC target(avx,avx2,fma)
| ^~~
islands.cpp:6:24: warning: '#pragma GCC option' is not a string [-Wpragmas]
6 | #pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native)
| ^~~
islands.cpp:26:43: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
26 | #pragma GCC optimize("-fwhole-program")
| ^
islands.cpp:33:45: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
33 | #pragma GCC optimize("-fstrict-overflow")
| ^
islands.cpp:35:45: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
35 | #pragma GCC optimize("-fcse-skip-blocks")
| ^
islands.cpp:49:55: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
49 | #pragma GCC optimize("-funsafe-loop-optimizations")
| ^
islands.cpp:63:36: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
63 | void add_edge(int u , int v , int w){
| ^
islands.cpp:63:36: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:63:36: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:63:36: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:67:26: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
67 | void search(int u , int p){
| ^
islands.cpp:67:26: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:67:26: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:67:26: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:93:30: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
93 | void dfs(int u , int p , ll s){
| ^
islands.cpp:93:30: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:93:30: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:93:30: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
islands.cpp:106:14: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
106 | int32_t main(){
| ^
islands.cpp:106:14: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
islands.cpp:106:14: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
islands.cpp:106:14: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
39772 KB |
Output is correct |
2 |
Correct |
6 ms |
39888 KB |
Output is correct |
3 |
Correct |
6 ms |
39908 KB |
Output is correct |
4 |
Correct |
6 ms |
40012 KB |
Output is correct |
5 |
Correct |
8 ms |
39776 KB |
Output is correct |
6 |
Correct |
6 ms |
39772 KB |
Output is correct |
7 |
Correct |
6 ms |
39924 KB |
Output is correct |
8 |
Correct |
6 ms |
39772 KB |
Output is correct |
9 |
Correct |
6 ms |
39780 KB |
Output is correct |
10 |
Correct |
7 ms |
39776 KB |
Output is correct |
11 |
Correct |
6 ms |
40020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
40028 KB |
Output is correct |
2 |
Correct |
6 ms |
40028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
40028 KB |
Output is correct |
2 |
Correct |
7 ms |
40284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
40796 KB |
Output is correct |
2 |
Correct |
17 ms |
42844 KB |
Output is correct |
3 |
Correct |
17 ms |
40796 KB |
Output is correct |
4 |
Correct |
9 ms |
40300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
44124 KB |
Output is correct |
2 |
Correct |
27 ms |
45660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
49748 KB |
Output is correct |
2 |
Correct |
58 ms |
60144 KB |
Output is correct |
3 |
Correct |
81 ms |
74332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
56088 KB |
Output is correct |
2 |
Correct |
143 ms |
92244 KB |
Output is correct |
3 |
Correct |
165 ms |
103468 KB |
Output is correct |
4 |
Correct |
197 ms |
127260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
83608 KB |
Output is correct |
2 |
Runtime error |
529 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
262 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |