#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef int ll;
typedef long long int lld;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro "<<i<<endl
#define INF 1000000000000000000LL
#define EPS ((ld)0.00000000001)
#define pi ((ld)3.141592653589793)
#define VV(vvvv,NNNN,xxxx); REP(iiiii,0,NNNN) {vvvv.pb(xxxx);}
ll mod=1000000007;
template<class A=ll>
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}
template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}}
vector<ll> f, w; vector<bool> visited;
vector<vector<ll> > adj; ll nei;
lld bestpath;
vector<lld> rm; vector<lld> ps;
vector<ll> Floyd(ll s) //Floyd's Algorithm, O(N) time, O(1) memory, return <element of cycle,length od cycle>
{
ll a=f[s]; ll b=f[f[s]];
while(a!=b) {a=f[a]; b=f[f[b]];}
vector<ll> ans; ans.pb(a); b=f[a];
while(b!=a) {ans.pb(b); b=f[b];}
return ans;
}
lld DFS(ll s)
{
visited[s]=true; lld ans = 0; lld thisval;
REP(i,0,adj[s].size())
{
nei = adj[s][i];
if(visited[nei]) {continue;}
thisval = (lld)(w[nei]+DFS(nei));
bestpath = max(bestpath,ans+thisval);
ans = max(ans,thisval);
}
return ans;
}
void Rolling_Max(ll M, ll L) //ans[i]=j means aj<ai, j<i and j is the largest that fullfills these conditions. If no j exists then a[i]=-1.
{
deque<ll> chain;
REP(i,0,L)
{
while(chain.size()>0 && ps[i]>=ps[chain[chain.size()-1]]) {chain.pop_back();}
chain.pb(i);
if(chain.front()==i-M) {chain.pop_front();}
rm[i]=ps[chain.front()];
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cout.precision(20);
ll N; cin>>N; f = vector<ll>(N,-1); w = vector<ll>(N,-1); visited = vector<bool>(N,false); adj=vector<vector<ll> >(N,vector<ll>());
REP(i,0,N) {cin>>f[i]>>w[i]; f[i]--; adj[i].pb(f[i]); adj[f[i]].pb(i);}
lld ans = 0LL; ll node; ll C; rm = vector<lld>(2*N,0); ps = vector<lld>(2*N,0);
cout<<0<<endl; return 0;
REP(s,0,N)
{
if(visited[s]) {continue;}
lld bestval = 0; lld thisval;
vector<ll> cycle = Floyd(s); C = cycle.size();
REP(i,0,cycle.size()) {node=cycle[i]; visited[node]=true;}
vector<lld> val(C,0);
REP(i,0,cycle.size())
{
bestpath = 0;
val[i]=DFS(cycle[i]);
bestval = max(bestval,bestpath);
}
lld cursum=0;
REP(i,0,2*C) {node = cycle[i%C]; ps[i]=(cursum+val[i%C]); cursum+=w[node];}
Rolling_Max(C-1,2*C);
REP(i,0,C)
{
thisval=rm[i+C-1] -(ps[i]-val[i])+val[i];
bestval = max(thisval,bestval);
}
ans+=bestval;
}
cout<<ans<<endl;
return 0;
}
Compilation message
islands.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization ("O3")
|
islands.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization ("unroll-loops")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
596 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
19292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
38228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
165 ms |
98308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
171 ms |
94388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |