#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000001;
int u[N];
int vidx[N];
ll dp[N];
ll down[N];
array<int,3> v[2*N];
int vsz=0;
int badidx=2*N;
int cycle[N];
int cyclesz=0;
int in_cycle=0;
int find_set(int a)
{
if(a==u[a]) return a;
else return u[a]=find_set(u[a]);
}
int merge_sets(int a,int b)
{
a=find_set(a);
b=find_set(b);
if(a==b) return 0;
u[b]=a;
return 1;
}
void chmax(ll &a,ll b){a=max(a,b);}
void dfs(int a)
{
cycle[cyclesz++]=a;
ll one=0,two=0;
for(int i=vidx[a];i<vsz&&v[i][0]==a;i++)
{
auto [_,to,c]=v[i];
if(to==u[a]) continue;
u[to]=a;
dfs(to);
ll len=c+down[to];
if(len>one){two=one; one=len;}
else if(len>two){two=len;}
chmax(dp[a],dp[to]);
chmax(down[a],len);
}
chmax(dp[a],one+two);
}
void go(int a,int p)
{
for(int i=vidx[a];i<vsz&&v[i][0]==a;i++)
{
auto [_,to,c]=v[i];
if(to==p||to==in_cycle) continue;
go(to,a);
chmax(down[a],c+down[to]);
}
}
int ucost(int i)
{
for(int j=vidx[cycle[i]];j<vsz&&v[j][0]==cycle[i];j++)
{
auto [_,to,c]=v[j];
if(to==cycle[i+1]) return c;
}
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;i++) u[i]=i;
for(int a=1;a<=n;a++)
{
int b,c;
cin >> b >> c;
if(merge_sets(a,b))
{
v[vsz++]={a,b,c};
v[vsz++]={b,a,c};
}
else v[--badidx]={a,b,c};
}
sort(v,v+vsz);
for(int i=vsz-1;i>=0;i--) vidx[v[i][0]]=i;
ll res=0;
memset(u,0,sizeof(u));
for(int o=2*N-1;o>=badidx;o--)
{
auto [x,y,bad_cost]=v[o];
dfs(x);
ll now=dp[x];
for(int i=0;i<cyclesz;i++) down[cycle[i]]=dp[cycle[i]]=0;
cyclesz=0;
int t=y;
while(t!=0)
{
cycle[cyclesz++]=t;
t=u[t];
}
for(int i=0;i<cyclesz;i++)
{
if(i+1<cyclesz) in_cycle=cycle[i+1];
go(cycle[i],(i>0?cycle[i-1]:0));
}
ll path=0;
for(int i=0;i<cyclesz;i++)
{
if(i>0) dp[cycle[i]]=dp[cycle[i-1]];
chmax(dp[cycle[i]],path+down[cycle[i]]);
if(i+1<cyclesz) path+=ucost(i);
}
path=0;
ll opt=0;
for(int i=cyclesz-1;i>=0;i--)
{
if(i+1<cyclesz) path+=ucost(i);
chmax(opt,path+down[cycle[i]]);
if(i>0) chmax(now,dp[cycle[i-1]]+bad_cost+opt);
}
res+=now;
cyclesz=0;
}
cout << res << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4168 KB |
Output is correct |
3 |
Correct |
2 ms |
4308 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5172 KB |
Output is correct |
2 |
Correct |
18 ms |
7336 KB |
Output is correct |
3 |
Correct |
15 ms |
5460 KB |
Output is correct |
4 |
Correct |
6 ms |
4872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
8468 KB |
Output is correct |
2 |
Correct |
34 ms |
10344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
15528 KB |
Output is correct |
2 |
Correct |
77 ms |
20948 KB |
Output is correct |
3 |
Correct |
93 ms |
28060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
23036 KB |
Output is correct |
2 |
Correct |
172 ms |
43704 KB |
Output is correct |
3 |
Correct |
193 ms |
52048 KB |
Output is correct |
4 |
Correct |
225 ms |
64988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
402 ms |
55824 KB |
Output is correct |
2 |
Correct |
569 ms |
90568 KB |
Output is correct |
3 |
Correct |
250 ms |
40156 KB |
Output is correct |
4 |
Correct |
324 ms |
72960 KB |
Output is correct |
5 |
Correct |
315 ms |
73684 KB |
Output is correct |
6 |
Correct |
777 ms |
48592 KB |
Output is correct |
7 |
Correct |
339 ms |
97612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
131072 KB |
Output is correct |
2 |
Correct |
361 ms |
90588 KB |
Output is correct |
3 |
Correct |
400 ms |
131072 KB |
Output is correct |
4 |
Correct |
280 ms |
41704 KB |
Output is correct |
5 |
Correct |
312 ms |
73416 KB |
Output is correct |
6 |
Correct |
345 ms |
57380 KB |
Output is correct |
7 |
Correct |
790 ms |
49464 KB |
Output is correct |