#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> u(n+1,0);
for(int i=1;i<=n;i++) u[i]=i;
function<int(int)> find_set=[&](int a)->int
{
if(a==u[a]) return a;
else return u[a]=find_set(u[a]);
};
auto merge_sets=[&](int a,int b)->int
{
a=find_set(a);
b=find_set(b);
if(a==b) return 0;
u[b]=a;
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);};
u.assign(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);
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]=dp[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) dp[cycle[i]]=dp[cycle[i-1]];
chmax(dp[cycle[i]],path+down[cycle[i]]);
path+=ucost[cycle[i]];
}
path=0;
ll opt=0;
for(int i=len-1;i>=0;i--)
{
path+=ucost[cycle[i]];
chmax(opt,path+down[cycle[i]]);
if(i>0) chmax(now,dp[cycle[i-1]]+bad_cost+opt);
}
res+=now;
}
cout << res << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1352 KB |
Output is correct |
2 |
Correct |
24 ms |
4164 KB |
Output is correct |
3 |
Correct |
12 ms |
1740 KB |
Output is correct |
4 |
Correct |
7 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5452 KB |
Output is correct |
2 |
Correct |
37 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
14368 KB |
Output is correct |
2 |
Correct |
95 ms |
20556 KB |
Output is correct |
3 |
Correct |
121 ms |
29968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
22540 KB |
Output is correct |
2 |
Correct |
215 ms |
48764 KB |
Output is correct |
3 |
Correct |
241 ms |
58800 KB |
Output is correct |
4 |
Correct |
272 ms |
76164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
496 ms |
75196 KB |
Output is correct |
2 |
Correct |
713 ms |
120608 KB |
Output is correct |
3 |
Correct |
302 ms |
43056 KB |
Output is correct |
4 |
Correct |
386 ms |
84884 KB |
Output is correct |
5 |
Correct |
390 ms |
86568 KB |
Output is correct |
6 |
Correct |
909 ms |
56504 KB |
Output is correct |
7 |
Correct |
446 ms |
131072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
441 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |