#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define MEM(i,j) memset(i,j,sizeof i)
#define F first
#define S second
#define MP make_pair
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
struct mode
{
int F,S,num;
};
vector<mode> G[1000005];
vector<pii> cir;
vector<ll> len;
deque<pll> dq;
bitset<1000005> yc;
ll M;
int dft,dfn[1000005];
const ll INF=1e18;
bool dfs(int u,int f)
{
int flag=0;
dfn[u]=++dft;
for(auto i:G[u])
if(i.num!=f)
if(!dfn[i.F])
if(dfs(i.F,i.num))
flag=1,cir.back().S=i.S;
else;
else if(dfn[i.F]<=dfn[u])
flag=1,yc[i.F]=1,cir.pb(MP(i.F,i.S)),len.pb(0);
if(flag)
if(yc[u]) flag=0;
else cir.pb(MP(u,0)),len.pb(0),yc[u]=1;
return flag;
}
ll dfs2(int u)
{
ll mx=0,sx=0;
yc[u]=1;
for(auto i:G[u])
if(!yc[i.F])
{
ll d=dfs2(i.F)+i.S;
if(d>mx) sx=mx,mx=d;
else if(d>sx) sx=d;
}
M=max(M,sx+mx);
return mx;
}
int main()
{jizz
int n,a,w;
ll ans=0,sum;
cin >> n;
for(int i=1;i<=n;++i)
{
cin >> a >> w;
G[i].pb(mode{a,w,i});
if(a!=i) G[a].pb(mode{i,w,i});
}
for(int i=1;i<=n;++i)
if(!dfn[i])
{
vector<pii>().swap(cir),vector<ll>().swap(len),M=0,dfs(i,-1);
deque<pll>().swap(dq);
for(int j=0;j<cir.size();++j)
len[j]=dfs2(cir[j].F);
if(cir.size()>1)
{
int n=cir.size();
sum=0,dq.pb(MP(0,len[0]));
for(int j=0;j<n;++j)
cir.pb(cir[j]),len.pb(len[j]);
for(int j=1;j<cir.size();++j)
{
while(dq.size()&&j-dq[0].F>=n) dq.pop_front();
sum+=cir[j-1].S,M=max(M,sum+len[j]+dq[0].S);
while(dq.size()&&len[j]-sum>=dq.back().S)
dq.pop_back();
dq.pb(MP(j,len[j]-sum));
}
}
ans+=M;
}
cout << ans << "\n";
}
Compilation message
islands.cpp: In function 'bool dfs(int, int)':
islands.cpp:35:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(i.num!=f)
^
islands.cpp:42:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(flag)
^
islands.cpp: In function 'int main()':
islands.cpp:79:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<cir.size();++j)
~^~~~~~~~~~~
islands.cpp:87:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=1;j<cir.size();++j)
~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23808 KB |
Output is correct |
2 |
Correct |
23 ms |
23808 KB |
Output is correct |
3 |
Correct |
25 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23808 KB |
Output is correct |
5 |
Correct |
23 ms |
23808 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
22 ms |
23808 KB |
Output is correct |
8 |
Correct |
21 ms |
23800 KB |
Output is correct |
9 |
Correct |
21 ms |
23936 KB |
Output is correct |
10 |
Correct |
22 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23936 KB |
Output is correct |
2 |
Correct |
22 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23936 KB |
Output is correct |
2 |
Correct |
24 ms |
24320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
25088 KB |
Output is correct |
2 |
Correct |
42 ms |
28020 KB |
Output is correct |
3 |
Correct |
31 ms |
24952 KB |
Output is correct |
4 |
Correct |
26 ms |
24392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
29444 KB |
Output is correct |
2 |
Correct |
61 ms |
31448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
36256 KB |
Output is correct |
2 |
Correct |
129 ms |
46716 KB |
Output is correct |
3 |
Correct |
155 ms |
59536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
182 ms |
43936 KB |
Output is correct |
2 |
Correct |
241 ms |
76752 KB |
Output is correct |
3 |
Correct |
287 ms |
90936 KB |
Output is correct |
4 |
Correct |
346 ms |
115828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
321 ms |
71028 KB |
Output is correct |
2 |
Runtime error |
724 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
389 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |