#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 1000005
#define II pair <int,int>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x))
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
using namespace std;
struct edge { int u,num,k; };
vector <edge> a[N];
int tr[N],trc[N],incycle[N],cycle[N],visited[N];
ll n,i,u,v,k,c[N],dis[N],ncycle,sum,res;
const ll oo=round(1e18);
struct Fenwick_Tree
{
ll bit[N],d[N],n;
void Init(int _n)
{
n=_n;
for(int i=1;i<=n;i++) bit[i]=d[i]=-oo;
}
void update(int i,ll k)
{
d[i]=max(d[i],k);
while(i<=n) bit[i]=max(bit[i],k),i+=(i & -i);
}
ll get(int l,int r)
{
ll res=-oo,i=r;
while(i>=l)
if(i-(i & -i)>=l) res=max(res,bit[i]),i-=(i & -i);
else res=max(res,d[i]),i--;
return res;
}
} FT;
void dfs(int u)
{
visited[u]=1;
for(edge adj:a[u])
{
int v=adj.u;
if(visited[v]==0) dfs(v);
}
}
void Find_cycle(int u,int x)
{
for(edge adj:a[u])
{
if(adj.num==x) continue;
int v=adj.u,k=adj.k;
if(tr[v]==0)
{
tr[v]=u; trc[v]=k;
Find_cycle(v,adj.num);
}
else if(ncycle==0)
{
sum=0;
while(u!=v)
{
cycle[++ncycle]=u; incycle[u]=1;
dis[ncycle]=sum; sum+=trc[u];
u=tr[u];
}
cycle[++ncycle]=u; incycle[u]=1;
dis[ncycle]=sum; sum+=k;
return ;
}
}
}
/////////////////////////////////////////////////////////////////////////////
ll cal(int u,int p,ll &ma)
{
ll res=0;
vector <ll> vec;
for(edge adj:a[u])
{
int v=adj.u;
if(v!=p && incycle[v]==0)
{
ll k=cal(v,u,ma)+adj.k;
res=max(res,k);
vec.push_back(k);
}
}
sort(vec.rbegin(),vec.rend());
if(vec.size()>=2) ma=max(ma,vec[0]+vec[1]);
return res;
}
ll Work()
{
int i,u,v;
FT.Init(ncycle);
ll res=0;
for(i=1;i<=ncycle;i++)
{
u=cycle[i];
c[i]=cal(u,0,res);
FT.update(i,c[i]-dis[i]);
}
for(i=1;i<ncycle;i++)
{
int l=i,r=ncycle;
while(l<r)
{
int mid=(l+r+1)>>1;
if(sum-(dis[mid]-dis[i])>=dis[mid]-dis[i])
l=mid;
else r=mid-1;
}
res=max(res,c[i]+sum+dis[i]+FT.get(i+1,l));
}
FT.Init(ncycle);
for(i=1;i<=ncycle;i++)
{
u=cycle[i];
FT.update(i,c[i]+dis[i]);
}
for(i=1;i<ncycle;i++)
{
int l=i,r=ncycle;
while(l<r)
{
int mid=(l+r+1)>>1;
if(sum-(dis[mid]-dis[i])>=dis[mid]-dis[i])
l=mid;
else r=mid-1;
}
res=max(res,c[i]-dis[i]+FT.get(l+1,ncycle));
}
return res;
}
int main()
{
// freopen("islands.inp","r",stdin);
// freopen("islands.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(u=1;u<=n;u++)
{
cin>>v>>k;
a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
}
for(u=1;u<=n;u++)
if(visited[u]==0)
{
dfs(u);
tr[u]=-1;
ncycle=0;
Find_cycle(u,0);
res+=Work();
}
cout<<res;
}
Compilation message
islands.cpp: In function 'long long int Work()':
islands.cpp:104:13: warning: unused variable 'v' [-Wunused-variable]
104 | int i,u,v;
| ^
islands.cpp: In function 'int main()':
islands.cpp:156:26: warning: narrowing conversion of 'v' from 'long long int' to 'int' [-Wnarrowing]
156 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:156:28: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
156 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:156:30: warning: narrowing conversion of 'k' from 'long long int' to 'int' [-Wnarrowing]
156 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:156:53: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
156 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:156:55: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
156 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:156:57: warning: narrowing conversion of 'k' from 'long long int' to 'int' [-Wnarrowing]
156 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
41564 KB |
Output is correct |
2 |
Correct |
7 ms |
41820 KB |
Output is correct |
3 |
Correct |
7 ms |
41820 KB |
Output is correct |
4 |
Correct |
7 ms |
41564 KB |
Output is correct |
5 |
Correct |
7 ms |
41564 KB |
Output is correct |
6 |
Correct |
7 ms |
41820 KB |
Output is correct |
7 |
Correct |
7 ms |
41688 KB |
Output is correct |
8 |
Correct |
7 ms |
41820 KB |
Output is correct |
9 |
Correct |
7 ms |
41808 KB |
Output is correct |
10 |
Correct |
8 ms |
41816 KB |
Output is correct |
11 |
Correct |
7 ms |
41808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
41688 KB |
Output is correct |
2 |
Correct |
7 ms |
41860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
41904 KB |
Output is correct |
2 |
Correct |
9 ms |
42076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
44632 KB |
Output is correct |
2 |
Correct |
22 ms |
46428 KB |
Output is correct |
3 |
Correct |
16 ms |
44636 KB |
Output is correct |
4 |
Correct |
11 ms |
44228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47452 KB |
Output is correct |
2 |
Correct |
40 ms |
51028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
55892 KB |
Output is correct |
2 |
Correct |
86 ms |
62800 KB |
Output is correct |
3 |
Correct |
115 ms |
73132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
64120 KB |
Output is correct |
2 |
Correct |
194 ms |
90780 KB |
Output is correct |
3 |
Correct |
251 ms |
92296 KB |
Output is correct |
4 |
Correct |
285 ms |
116276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
103044 KB |
Output is correct |
2 |
Runtime error |
610 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
286 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |