#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];
bitset<1000005> yc,vis;
ll M,tp;
pii edge[1000005];
pll cir[1000005];
int dq[1000005],dql,dqr;
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[tp-1].S=edge[i].S;
else;
else if(!yc[u])
flag=1,yc[i]=1,cir[tp++]=MP(i,edge[i].S);
if(u!=f)
if(!vis[edge[u].F])
if(dfs(edge[u].F,u))
flag=1,cir[tp-1].S=edge[u].S;
else;
else if(!yc[u])
flag=1,yc[edge[u].F]=1,cir[tp++]=MP(edge[u].F,edge[u].S);
if(flag)
if(yc[u]) flag=0;
else cir[tp++]=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;
}
ll sum(int l)
{
ll rt=0;
if(l>tp)
rt=cir[tp-1].S,l-=tp;
if(l)
rt+=cir[l-1].S;
return rt;
}
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])
{
tp=0,M=0,dql=0,dqr=-1,dfs(i,-1);
for(int j=0;j<tp;++j)
cir[j].F=dfs2(cir[j].F);
for(int j=1;j<tp;++j)
cir[j].S+=cir[j-1].S;
if(tp>1)
{
dq[++dqr]=0;
for(int j=1;j<2*tp;++j)
{
while(dqr>=dql&&j-dq[dql%tp]>=tp) ++dql;
M=max(M,sum(j)+cir[j%tp].F+cir[dq[dql%tp]%tp].F-sum(dq[dql%tp]));
while(dqr>=dql&&cir[j%tp].F-sum(j)>=cir[dq[dqr%tp]%tp].F-sum(dq[dqr%tp]))
--dqr;
while(dqr>=dql&&j+1-dq[dql%tp]>=tp) ++dql;
dq[(++dqr)%tp]=j;
}
}
ans+=M;
}
printf("%lld\n",ans);
}
Compilation message
islands.cpp: In function 'bool dfs(int, int)':
islands.cpp:29:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(i!=f)
^
islands.cpp:36:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(u!=f)
^
islands.cpp:43:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(flag)
^
islands.cpp: In function 'int main()':
islands.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
islands.cpp:88: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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
22 ms |
23808 KB |
Output is correct |
3 |
Correct |
22 ms |
23936 KB |
Output is correct |
4 |
Correct |
24 ms |
23936 KB |
Output is correct |
5 |
Correct |
23 ms |
23936 KB |
Output is correct |
6 |
Correct |
24 ms |
23680 KB |
Output is correct |
7 |
Correct |
25 ms |
23808 KB |
Output is correct |
8 |
Correct |
23 ms |
23772 KB |
Output is correct |
9 |
Correct |
23 ms |
23804 KB |
Output is correct |
10 |
Correct |
23 ms |
23808 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23936 KB |
Output is correct |
2 |
Correct |
24 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23936 KB |
Output is correct |
2 |
Correct |
25 ms |
24192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
24448 KB |
Output is correct |
2 |
Correct |
41 ms |
26488 KB |
Output is correct |
3 |
Correct |
33 ms |
24440 KB |
Output is correct |
4 |
Correct |
26 ms |
24064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
27256 KB |
Output is correct |
2 |
Correct |
65 ms |
28860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
31180 KB |
Output is correct |
2 |
Correct |
136 ms |
41020 KB |
Output is correct |
3 |
Correct |
152 ms |
47516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
37344 KB |
Output is correct |
2 |
Correct |
290 ms |
57848 KB |
Output is correct |
3 |
Correct |
329 ms |
76656 KB |
Output is correct |
4 |
Correct |
340 ms |
85368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
36156 KB |
Output is correct |
2 |
Correct |
1029 ms |
104904 KB |
Output is correct |
3 |
Correct |
347 ms |
46712 KB |
Output is correct |
4 |
Correct |
474 ms |
83188 KB |
Output is correct |
5 |
Correct |
458 ms |
80796 KB |
Output is correct |
6 |
Correct |
1245 ms |
51140 KB |
Output is correct |
7 |
Correct |
520 ms |
117368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
408 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |