#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 long long int ll;
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;
ll bestpath;
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;
}
ll DFS(ll s)
{
visited[s]=true; ll ans = 0; ll thisval;
REP(i,0,adj[s].size())
{
nei = adj[s][i];
if(visited[nei]) {continue;}
thisval = w[nei]+DFS(nei);
bestpath = max(bestpath,ans+thisval);
ans = max(ans,thisval);
}
return ans;
}
template<class T=ll>
class SparseTable //Range Minimum Queries
{
public:
ll N;
vector<T> a;
vector<vector<T> > v;
SparseTable() {N=0LL;}
SparseTable(vector<T> b)
{
a=b; N=a.size();
ll lo=(ll) floor((ld) log2(N)) +1LL;
vector<T> xx;
REP(i,0,lo) {xx.pb(0);} REP(i,0,N) {v.pb(xx);}
REP(step,0LL,lo)
{
REP(i,0,N-(1LL<<step)+1LL)
{
if(step==0) {v[i][0]=a[i];}
else {v[i][step]=max(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);}
}
}
}
T query(ll l, ll r)
{
ll step=(ll) floor((ld) log2(r-l+1LL));
return max(v[l][step],v[r-(1LL<<step)+1LL][step]);
}
};
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);}
ll ans = 0LL; ll node; ll C;
REP(s,0,N)
{
if(visited[s]) {continue;}
ll bestval = 0; ll thisval;
vector<ll> cycle = Floyd(s); C = cycle.size();
REP(i,0,cycle.size()) {node=cycle[i]; visited[node]=true;}
vector<ll> val(C,0);
REP(i,0,cycle.size())
{
bestpath = 0;
val[i]=DFS(cycle[i]);
bestval = max(bestval,bestpath);
}
vector<ll> ps; ll cursum=0;
REP(i,0,2*C) {node = cycle[i%C]; ps.pb(cursum+val[i%C]); cursum+=w[node];}
SparseTable<ll> S(ps);
REP(i,0,C)
{
thisval=S.query(i+1,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 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2436 KB |
Output is correct |
2 |
Correct |
15 ms |
8880 KB |
Output is correct |
3 |
Correct |
6 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
12744 KB |
Output is correct |
2 |
Correct |
32 ms |
16640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
25792 KB |
Output is correct |
2 |
Correct |
95 ms |
52096 KB |
Output is correct |
3 |
Correct |
134 ms |
87660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
41268 KB |
Output is correct |
2 |
Correct |
197 ms |
129336 KB |
Output is correct |
3 |
Runtime error |
142 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
94200 KB |
Output is correct |
2 |
Runtime error |
358 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
131072 KB |
Output is correct |
2 |
Correct |
225 ms |
109936 KB |
Output is correct |
3 |
Runtime error |
249 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |