#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<array<int,3>> v;
vector<array<int,3>> bad_edges;
vector<int> dsu_p(n+1,0);
for(int i=1;i<=n;i++) dsu_p[i]=i;
vector<int> dsu_sz(n+1,1);
function<int(int)> find_set=[&](int a)->int
{
if(a==dsu_p[a]) return a;
else return dsu_p[a]=find_set(dsu_p[a]);
};
auto merge_sets=[&](int a,int b)->int
{
a=find_set(a);
b=find_set(b);
if(a==b) return 0;
if(dsu_sz[a]<dsu_sz[b]) swap(a,b);
dsu_p[b]=a;
dsu_sz[a]+=dsu_sz[b];
return 1;
};
for(int a=1;a<=n;a++)
{
int b,c;
cin >> b >> c;
if(merge_sets(a,b))
{
v.push_back({a,b,c});
v.push_back({b,a,c});
}
else bad_edges.push_back({a,b,c});
}
sort(v.begin(),v.end());
vector<int> vidx(n+1,0);
for(int i=(int)v.size()-1;i>=0;i--) vidx[v[i][0]]=i;
ll res=0;
auto chmax=[&](ll &a,ll b){a=max(a,b);};
vector<int> u(n+1,0);
vector<int> ucost(n+1,0);
vector<bool> in_cycle(n+1,0);
vector<ll> dp(n+1,0);
vector<ll> down(n+1,0);
vector<ll> lbest(n+1,0);
vector<ll> rbest(n+1,0);
for(auto [x,y,bad_cost]:bad_edges)
{
vector<int> cc;
vector<int> cycle;
function<void(int)> dfs=[&](int a)
{
cc.push_back(a);
ll one=0,two=0;
for(int i=vidx[a];i<(int)v.size()&&v[i][0]==a;i++)
{
auto [_,to,c]=v[i];
if(to==u[a]) continue;
u[to]=a;
ucost[to]=c;
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);
};
dfs(x);
ll now=dp[x];
int t=y;
while(t!=0)
{
cycle.push_back(t);
in_cycle[t]=1;
t=u[t];
}
for(int a:cc) down[a]=0;
function<void(int,int)> go=[&](int a,int p)
{
for(int i=vidx[a];i<(int)v.size()&&v[i][0]==a;i++)
{
auto [_,to,c]=v[i];
if(to==p||in_cycle[to]) continue;
go(to,a);
chmax(down[a],c+down[to]);
}
};
for(int a:cycle) go(a,0);
int len=cycle.size();
ll path=0;
for(int i=0;i<len;i++)
{
if(i>0) lbest[cycle[i]]=lbest[cycle[i-1]];
chmax(lbest[cycle[i]],path+down[cycle[i]]);
path+=ucost[cycle[i]];
}
path=0;
for(int i=len-1;i>=0;i--)
{
path+=ucost[cycle[i]];
if(i+1<len) rbest[cycle[i]]=rbest[cycle[i+1]];
chmax(rbest[cycle[i]],path+down[cycle[i]]);
if(i>0) chmax(now,lbest[cycle[i-1]]+bad_cost+rbest[cycle[i]]);
}
res+=now;
}
cout << res << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1876 KB |
Output is correct |
2 |
Correct |
21 ms |
4956 KB |
Output is correct |
3 |
Correct |
12 ms |
2388 KB |
Output is correct |
4 |
Correct |
8 ms |
1320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6548 KB |
Output is correct |
2 |
Correct |
41 ms |
9952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
19200 KB |
Output is correct |
2 |
Correct |
91 ms |
25900 KB |
Output is correct |
3 |
Correct |
115 ms |
35872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
32044 KB |
Output is correct |
2 |
Correct |
191 ms |
60576 KB |
Output is correct |
3 |
Correct |
230 ms |
72960 KB |
Output is correct |
4 |
Correct |
282 ms |
91104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
460 ms |
85560 KB |
Output is correct |
2 |
Correct |
637 ms |
131072 KB |
Output is correct |
3 |
Correct |
319 ms |
72220 KB |
Output is correct |
4 |
Correct |
411 ms |
120768 KB |
Output is correct |
5 |
Correct |
428 ms |
122080 KB |
Output is correct |
6 |
Correct |
984 ms |
93760 KB |
Output is correct |
7 |
Runtime error |
410 ms |
131072 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
364 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |