#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;
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[edge[i].F^u])
if(dfs(edge[i].F^u,i))
flag=1,cir.back().S=edge[i].S;
else;
else if(!yc[u])
flag=1,yc[edge[i].F^u]=1,cir.pb(MP(edge[i].F^u,edge[i].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[edge[i].F^u])
{
ll d=dfs2(edge[i].F^u)+edge[i].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()
{jizz
int n;
ll ans=0,sum;
cin >> n;
for(int i=1;i<=n;++i)
{
cin >> edge[i].F >> edge[i].S,edge[i].F^=i;
G[i].pb(i);
if(edge[i].F!=0) G[edge[i].F^i].pb(i);
}
for(int i=1;i<=n;++i)
if(!vis[i])
{
vector<pll>().swap(cir),M=0,dfs(i,-1);
deque<int>().swap(dq);
for(int j=0;j<cir.size();++j)
cir[j].F=dfs2(cir[j].F);
if(cir.size()>1)
{
int n=cir.size();
sum=0,dq.pb(0);
for(int j=0;j<n;++j)
cir.pb(cir[j]);
for(int j=1;j<cir.size();++j)
{
while(dq.size()&&j-dq[0]>=n) dq.pop_front();
sum+=cir[j-1].S,M=max(M,sum+cir[j].F+cir[dq[0]].F);
cir[j].F-=sum;
while(dq.size()&&cir[j].F>=cir[dq.back()].F)
dq.pop_back();
dq.pb(j);
}
}
ans+=M;
}
cout << ans << "\n";
}
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(flag)
^
islands.cpp: In function 'int main()':
islands.cpp:74:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<cir.size();++j)
~^~~~~~~~~~~
islands.cpp:82: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 |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23796 KB |
Output is correct |
3 |
Correct |
24 ms |
23936 KB |
Output is correct |
4 |
Correct |
23 ms |
23808 KB |
Output is correct |
5 |
Correct |
28 ms |
23804 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23808 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
22 ms |
23808 KB |
Output is correct |
10 |
Correct |
23 ms |
23936 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23936 KB |
Output is correct |
2 |
Correct |
26 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23956 KB |
Output is correct |
2 |
Correct |
25 ms |
24320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
25080 KB |
Output is correct |
2 |
Correct |
82 ms |
27580 KB |
Output is correct |
3 |
Correct |
37 ms |
24960 KB |
Output is correct |
4 |
Correct |
29 ms |
24440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
29304 KB |
Output is correct |
2 |
Correct |
68 ms |
31760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
36148 KB |
Output is correct |
2 |
Correct |
141 ms |
46180 KB |
Output is correct |
3 |
Correct |
167 ms |
59568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
184 ms |
43280 KB |
Output is correct |
2 |
Correct |
253 ms |
71072 KB |
Output is correct |
3 |
Correct |
297 ms |
84848 KB |
Output is correct |
4 |
Correct |
345 ms |
100704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
68288 KB |
Output is correct |
2 |
Correct |
1387 ms |
131072 KB |
Output is correct |
3 |
Correct |
334 ms |
58060 KB |
Output is correct |
4 |
Correct |
482 ms |
113644 KB |
Output is correct |
5 |
Correct |
474 ms |
105272 KB |
Output is correct |
6 |
Correct |
1753 ms |
62968 KB |
Output is correct |
7 |
Runtime error |
463 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
525 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |