This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |