#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,2>> v[n+1];
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[a].push_back({b,c});
v[b].push_back({a,c});
}
else bad_edges.push_back({a,b,c});
}
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(auto [to,c]:v[a])
{
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(auto [to,c]:v[a])
{
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;
}
# |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
444 KB |
Output is correct |
2 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2260 KB |
Output is correct |
2 |
Correct |
18 ms |
6404 KB |
Output is correct |
3 |
Correct |
11 ms |
3028 KB |
Output is correct |
4 |
Correct |
6 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
8672 KB |
Output is correct |
2 |
Correct |
38 ms |
14120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
27040 KB |
Output is correct |
2 |
Correct |
87 ms |
34416 KB |
Output is correct |
3 |
Correct |
120 ms |
47600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
49816 KB |
Output is correct |
2 |
Correct |
214 ms |
82772 KB |
Output is correct |
3 |
Correct |
200 ms |
94764 KB |
Output is correct |
4 |
Correct |
263 ms |
121112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
334 ms |
131072 KB |
Output is correct |
2 |
Runtime error |
468 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
333 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |