#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;
vector<int> G[1000005];
vector<pll> cir;
vector<ll> sum;
deque<int> dq;
bitset<1000005> yc,vis;
ll M;
pii edge[1000005];
const ll INF=1e18;
bool dfs(int u,int f)
{
int flag=0;
vis[u]=1;
for(int i:G[u])
if(i!=f)
if(!vis[i])
if(dfs(i,i))
flag=1,cir.back().S=edge[i].S;
else;
else if(!yc[u])
flag=1,yc[i]=1,cir.pb(MP(i,edge[i].S));
if(u!=f)
if(!vis[edge[u].F])
if(dfs(edge[u].F,u))
flag=1,cir.back().S=edge[u].S;
else;
else if(!yc[u])
flag=1,yc[edge[u].F]=1,cir.pb(MP(edge[u].F,edge[u].S));
if(flag)
if(yc[u]) flag=0;
else cir.pb(MP(u,0)),yc[u]=1;
return flag;
}
ll dfs2(int u)
{
ll mx=0,sx=0;
yc[u]=1;
for(int i:G[u])
if(!yc[i])
{
ll d=dfs2(i)+edge[i].S;
if(d>mx) sx=mx,mx=d;
else if(d>sx) sx=d;
}
if(!yc[edge[u].F])
{
ll d=dfs2(edge[u].F)+edge[u].S;
if(d>mx) sx=mx,mx=d;
else if(d>sx) sx=d;
}
M=max(M,sx+mx);
vector<int>().swap(G[u]);
return mx;
}
int main()
{
int n;
ll ans=0;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d%d",&edge[i].F,&edge[i].S);
if(edge[i].F!=i) G[edge[i].F].pb(i);
}
for(int i=1;i<=n;++i)
if(!vis[i])
{
vector<pll>().swap(cir),vector<ll>().swap(sum),M=0,dfs(i,-1);
deque<int>().swap(dq);
for(int j=0;j<cir.size();++j)
cir[j].F=dfs2(cir[j].F);
sum.resize(cir.size());
if(cir.size()>1)
{
int n=cir.size();
sum[0]=0,dq.pb(0);
for(int j=1;j<2*n;++j)
{
while(dq.size()&&j-dq[0]>=n) dq.pop_front();
sum[j%n]=sum[(j-1)%n]+cir[(j-1)%n].S,M=max(M,sum[j%n]+cir[j%n].F+cir[dq[0]%n].F-sum[dq[0]%n]);
while(dq.size()&&cir[j%n].F-sum[j%n]>=cir[dq.back()%n].F-sum[dq.back()%n])
dq.pop_back();
dq.pb(j);
}
}
ans+=M;
}
printf("%lld\n",ans);
}
Compilation message
islands.cpp: In function 'bool dfs(int, int)':
islands.cpp:30:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(i!=f)
^
islands.cpp:37:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(u!=f)
^
islands.cpp:44:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(flag)
^
islands.cpp: In function 'int main()':
islands.cpp:87:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<cir.size();++j)
~^~~~~~~~~~~
islands.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
islands.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&edge[i].F,&edge[i].S);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23936 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23936 KB |
Output is correct |
4 |
Correct |
51 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23780 KB |
Output is correct |
6 |
Correct |
23 ms |
23808 KB |
Output is correct |
7 |
Correct |
22 ms |
23800 KB |
Output is correct |
8 |
Correct |
38 ms |
23784 KB |
Output is correct |
9 |
Correct |
23 ms |
23848 KB |
Output is correct |
10 |
Correct |
23 ms |
23936 KB |
Output is correct |
11 |
Correct |
23 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23936 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23932 KB |
Output is correct |
2 |
Correct |
25 ms |
24192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24696 KB |
Output is correct |
2 |
Correct |
45 ms |
26744 KB |
Output is correct |
3 |
Correct |
35 ms |
24544 KB |
Output is correct |
4 |
Correct |
27 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
27892 KB |
Output is correct |
2 |
Correct |
60 ms |
29056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
31756 KB |
Output is correct |
2 |
Correct |
125 ms |
43060 KB |
Output is correct |
3 |
Correct |
143 ms |
51268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
37624 KB |
Output is correct |
2 |
Correct |
227 ms |
61872 KB |
Output is correct |
3 |
Correct |
282 ms |
83776 KB |
Output is correct |
4 |
Correct |
318 ms |
92288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
276 ms |
36048 KB |
Output is correct |
2 |
Correct |
944 ms |
111144 KB |
Output is correct |
3 |
Correct |
311 ms |
47228 KB |
Output is correct |
4 |
Correct |
421 ms |
90988 KB |
Output is correct |
5 |
Correct |
416 ms |
88676 KB |
Output is correct |
6 |
Correct |
1236 ms |
51192 KB |
Output is correct |
7 |
Runtime error |
556 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
394 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |