#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define endl "\n"
typedef pair<int, int> Pii;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(x) begin(x), end(x)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(s) (s).begin(),(s).end()
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define FOR_(n) for (ll _ = 0; (_) < (ll)(n); ++(_))
#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define se second
#define pb push_back
#define P pair<ll,ll>
#define PQminll priority_queue<ll, vector<ll>, greater<ll>>
#define PQmaxll priority_queue<ll,vector<ll>,less<ll>>
#define PQminP priority_queue<P, vector<P>, greater<P>>
#define PQmaxP priority_queue<P,vector<P>,less<P>>
#define NP next_permutation
//const ll mod = 1000000009;
const ll mod = 998244353;
//const ll mod = 1000000007;
const ll inf = 4100000000000000000ll;
const ld eps = ld(0.00000000001);
static const long double pi = 3.141592653589793;
template<class T>void vcin(vector<T> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
template<class T,class K>void vcin(vector<T> &n,vector<K> &m){for(int i=0;i<int(n.size());i++) cin>>n[i]>>m[i];}
template<class T>void vcout(vector<T> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
template<class T>void vcin(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cin>>n[i][j];}}}
template<class T>void vcout(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cout<<n[i][j]<<" ";}cout<<endl;}cout<<endl;}
void yes(bool a){cout<<(a?"yes":"no")<<endl;}
void YES(bool a){cout<<(a?"YES":"NO")<<endl;}
void Yes(bool a){cout<<(a?"Yes":"No")<<endl;}
void possible(bool a){ cout<<(a?"possible":"impossible")<<endl; }
void Possible(bool a){ cout<<(a?"Possible":"Impossible")<<endl; }
void POSSIBLE(bool a){ cout<<(a?"POSSIBLE":"IMPOSSIBLE")<<endl; }
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
template<class T>auto min(const T& a){ return *min_element(all(a)); }
template<class T>auto max(const T& a){ return *max_element(all(a)); }
template<class T,class F>void print(pair<T,F> a){cout<<a.fi<<" "<<a.se<<endl;}
template<class T>bool chmax(T &a,const T b) { if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a,const T b) { if (b<a) { a=b; return 1; } return 0;}
template<class T> void ifmin(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
template<class T> void ifmax(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
ll fastgcd(ll u,ll v){ll shl=0;while(u&&v&&u!=v){bool eu=!(u&1);bool ev=!(v&1);if(eu&&ev){++shl;u>>=1;v>>=1;}else if(eu&&!ev){u>>=1;}else if(!eu&&ev){v>>=1;}else if(u>=v){u=(u-v)>>1;}else{ll tmp=u;u=(v-u)>>1;v=tmp;}}return !u?v<<shl:u<<shl;}
ll modPow(ll a, ll n, ll mod) { if(mod==1) return 0;ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
vector<ll> divisor(ll x){ vector<ll> ans; for(ll i = 1; i * i <= x; i++){ if(x % i == 0) {ans.push_back(i); if(i*i!=x){ ans.push_back(x / ans[i]);}}}sor(ans); return ans; }
ll pop(ll x){return __builtin_popcountll(x);}
ll poplong(ll x){ll y=-1;while(x){x/=2;y++;}return y;}
P hyou(P a){ll x=fastgcd(abs(a.fi),abs(a.se));a.fi/=x;a.se/=x;if(a.se<0){a.fi*=-1;a.se*=-1;}return a;}
P Pplus(P a,P b){ return hyou({a.fi*b.se+b.fi*a.se,a.se*b.se});}
P Ptimes(P a,ll b){ return hyou({a.fi*b,a.se});}
P Ptimes(P a,P b){ return hyou({a.fi*b.fi,a.se*b.se});}
P Pminus(P a,P b){ return hyou({a.fi*b.se-b.fi*a.se,a.se*b.se});}
P Pgyaku(P a){ return hyou({a.se,a.fi});}
template<class T>
struct Sum{
vector<T> data;
Sum(const vector<T>& v):data(v.size()+1){
for(ll i=0;i<v.size();i++) data[i+1]=data[i]+v[i];
}
T get(ll l,ll r) const {
return data[r]-data[l];
}
};
template<class T>
struct Sum2{
vector<vector<T>> data;
Sum2(const vector<vector<T>> &v):data(v.size()+1,vector<T>(v[0].size()+1)){
for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]=data[i][j+1]+v[i][j];
for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]+=data[i+1][j];
}
T get(ll x1,ll y1,ll x2,ll y2) const {
return data[x2][y2]+data[x1][y1]-data[x1][y2]-data[x2][y1];
}
};
void cincout(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout<< fixed << setprecision(15);
}
bool now[1000000],visited[1000000];
pair<int,int> g[1000000];
vector<pair<int,int>> g2[1000000];
vector<int> path;
bool cycle[1000000];
ll d[1000000];
ll C[1000000];
void dfs2(int a,int b,int c,ll D){
chmax(d[c],D);
multiset<ll> S;
for(auto e:g2[a]){
if(e.fi==b||cycle[e.fi]) continue;
dfs2(e.fi,a,c,D+e.se);
chmax(C[a],C[e.fi]+e.se);
S.insert(C[e.fi]+e.se);
}
if(S.size()>=2){
ll x=0;
auto itr=S.end();
itr--;
x+=(*itr);
itr--;
x+=(*itr);
chmax(C[c],x);
}
}
int main() {
cincout();
int n;
cin>>n;
ll ans=0;
for(int i=0;i<n;i++){
int p,q;
cin>>p>>q;
p--;
g[i]={p,q};
g2[p].pb({i,q});
}
for(int i=0;i<n;i++){
if(!visited[i]){
vector<int> path;
int p=i;
while(true){
visited[p]=true;
now[p]=true;
path.pb(p);
int q=g[p].fi;
if(cycle[q]) break;
if(now[q]){
for(int j=path.size()-1;j>=0;j--){
cycle[path[j]]=true;
if(path[j]==q) break;
}
break;
}
p=q;
}
}
}
for(int i=0;i<n;i++){
if(cycle[i]){
dfs2(i,-1,i,0);
}
}
vector<bool> memo(n);
vector<ll> p,q,a(n),b(n);
for(int i=0;i<n;i++){
if(cycle[i]){
if(memo[i]) continue;
p.clear();
q.clear();
int now=i,u=-1;
ll z=0;
while(true){
chmax(z,C[now]);
memo[now]=true;
auto e=g[now];
if(cycle[e.fi]){
q.pb(d[now]);
p.pb(e.se);
now=e.fi;
}
if(now==i) break;
}
{
ll ma=q[0];
ll ret=0;
for(int j=1;j<p.size();j++){
chmax(ret,ma+p[j-1]+q[j]);
ma=max(ma+p[j-1],q[j]);
}
chmax(z,ret);
}
{
ll m=p.size();
for(int j=0;j<m;j++) a[j]=b[j]=0;
for(int j=1;j<m;j++){
a[j]=a[j-1]+p[j-1];
}
for(int j=m-2;j>=0;j--){
b[j]=b[j+1]+p[j];
}
for(int j=0;j<m;j++){
a[j]+=q[j];
b[j]+=q[j];
}
for(int j=m-2;j>=0;j--) chmax(b[j],b[j+1]);
ll ret=0;
for(int j=0;j<m-1;j++) chmax(ret,a[j]+b[j+1]);
chmax(z,ret+p[m-1]);
}
ans+=z;
}
}
cout<<ans<<endl;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:184:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
184 | for(int j=1;j<p.size();j++){
| ~^~~~~~~~~
islands.cpp:168:17: warning: unused variable 'u' [-Wunused-variable]
168 | int now=i,u=-1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Execution timed out |
2061 ms |
23764 KB |
Time limit exceeded |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Incorrect |
10 ms |
23764 KB |
Output isn't correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
10 ms |
23764 KB |
Output is correct |
7 |
Correct |
10 ms |
23832 KB |
Output is correct |
8 |
Incorrect |
10 ms |
23764 KB |
Output isn't correct |
9 |
Correct |
11 ms |
23824 KB |
Output is correct |
10 |
Correct |
10 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
23892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2062 ms |
23872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2062 ms |
24788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
27312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2076 ms |
36724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
48628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
973 ms |
88304 KB |
Output is correct |
2 |
Correct |
440 ms |
88132 KB |
Output is correct |
3 |
Incorrect |
201 ms |
72532 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
225 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |