#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 100005
#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];
ll tr[N],trc[N],n,i,u,v,k,c[N],dis[N],ncycle,cycle[N],d[N],sum,incycle[N],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_sub,FT_sum;
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(1)
{
cycle[++ncycle]=u; incycle[u]=1;
dis[ncycle]=sum; sum+=trc[u];
if(u==v) break;
u=tr[u];
}
sum+=k;
return ;
}
}
}
/////////////////////////////////////////////////////////////////////////////
ll cal(int u,int p)
{
ll res=0;
for(edge adj:a[u])
{
int v=adj.u;
if(v!=p && incycle[v]==0) res=max(res,cal(v,u)+adj.k);
}
return res;
}
ll Work()
{
int i,u,v;
FT_sub.Init(ncycle);
FT_sum.Init(ncycle);
for(i=1;i<=ncycle;i++)
{
u=cycle[i];
c[i]=cal(u,0);
FT_sub.update(i,c[i]-dis[i]);
FT_sum.update(i,c[i]+dis[i]);
}
ll res=0;
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,max(c[i]+sum+dis[i]+FT_sub.get(i+1,l),c[i]-dis[i]+FT_sum.get(l,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(tr[u]==0)
{
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:87:13: warning: unused variable 'v' [-Wunused-variable]
87 | int i,u,v;
| ^
islands.cpp: In function 'int main()':
islands.cpp:122:26: warning: narrowing conversion of 'v' from 'long long int' to 'int' [-Wnarrowing]
122 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:122:28: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
122 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:122:30: warning: narrowing conversion of 'k' from 'long long int' to 'int' [-Wnarrowing]
122 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:122:53: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
122 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:122:55: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
122 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
islands.cpp:122:57: warning: narrowing conversion of 'k' from 'long long int' to 'int' [-Wnarrowing]
122 | a[u].push_back({ v,u,k }); a[v].push_back({ u,u,k });
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Runtime error |
10 ms |
22364 KB |
Execution killed with signal 11 |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Runtime error |
11 ms |
22292 KB |
Execution killed with signal 11 |
5 |
Runtime error |
11 ms |
22364 KB |
Execution killed with signal 11 |
6 |
Incorrect |
2 ms |
11100 KB |
Output isn't correct |
7 |
Runtime error |
11 ms |
22372 KB |
Execution killed with signal 11 |
8 |
Runtime error |
12 ms |
22408 KB |
Execution killed with signal 11 |
9 |
Runtime error |
11 ms |
22364 KB |
Execution killed with signal 11 |
10 |
Incorrect |
2 ms |
11100 KB |
Output isn't correct |
11 |
Correct |
2 ms |
11100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
11100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
22616 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
24156 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
15452 KB |
Output is correct |
2 |
Runtime error |
45 ms |
32384 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
12892 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
25 ms |
17652 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
9820 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
12176 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |