#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e6;
vector<pair<int,int>> e[N+10];
int f[N+10];
int l[N+10];
bool vis_fill[N+10];
bool vis_s[N+10];
bool on_cycle[N+10];
void dfs_fill(int x,vector<int> &vec)
{
vec.push_back(x);
vis_fill[x]=true;
for(auto v:e[x])
{
if(!vis_fill[v.fi])
dfs_fill(v.fi,vec);
}
return;
}
pair<long long,long long> dfs_tree(int x,int p)
{
pair<long long,long long> ans={0,0};
for(auto v:e[x])
{
if(on_cycle[v.fi] || v.fi==p)
continue;
auto tmp=dfs_tree(v.fi,x);
ans.fi=max({ans.fi,tmp.fi,ans.se+tmp.se+v.se});
ans.se=max(ans.se,tmp.se+v.se);
}
ans.fi=max(ans.fi,ans.se);
return ans;
}
long long solve(vector<int> &t)
{
long long ans=0;
int c;
for(int i=t[0];true;i=f[i])
{
if(vis_s[i])
{
c=i;
break;
}
vis_s[i]=true;
}
long long l_all=l[c];
on_cycle[c]=true;
for(int i=f[c];i!=c;l_all+=l[i],i=f[i])
on_cycle[i]=true;
long long mx=0,mn=0;
int x=c;
do{
x=f[x];
auto ww=dfs_tree(x,0);
ans=max(ans,ww.fi);
ans=max({ans,mx+ww.se,mn+ww.se});
mx=max(mx,ww.se)+l[x];
mn=max(mn,ww.se+l_all)-l[x];
}while(x!=c);
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
f[i]=a;
l[i]=b;
e[i].emplace_back(a,b);
e[a].emplace_back(i,b);
}
long long ans=0;
for(int i=1;i<=n;i++)
{
if(!vis_fill[i])
{
vector<int> tmp;
dfs_fill(i,tmp);
ans+=solve(tmp);
}
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23712 KB |
Output is correct |
2 |
Correct |
15 ms |
23776 KB |
Output is correct |
3 |
Correct |
17 ms |
23772 KB |
Output is correct |
4 |
Correct |
17 ms |
23740 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
17 ms |
23760 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23828 KB |
Output is correct |
9 |
Correct |
16 ms |
23756 KB |
Output is correct |
10 |
Correct |
16 ms |
23840 KB |
Output is correct |
11 |
Correct |
16 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23884 KB |
Output is correct |
2 |
Correct |
16 ms |
23868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23884 KB |
Output is correct |
2 |
Correct |
17 ms |
24176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
24600 KB |
Output is correct |
2 |
Correct |
33 ms |
26768 KB |
Output is correct |
3 |
Correct |
28 ms |
24932 KB |
Output is correct |
4 |
Correct |
20 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
27712 KB |
Output is correct |
2 |
Correct |
51 ms |
29708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
35444 KB |
Output is correct |
2 |
Correct |
97 ms |
39152 KB |
Output is correct |
3 |
Correct |
129 ms |
46508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
43408 KB |
Output is correct |
2 |
Correct |
199 ms |
62648 KB |
Output is correct |
3 |
Correct |
225 ms |
68700 KB |
Output is correct |
4 |
Correct |
279 ms |
81760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
79708 KB |
Output is correct |
2 |
Correct |
798 ms |
106368 KB |
Output is correct |
3 |
Correct |
300 ms |
62952 KB |
Output is correct |
4 |
Correct |
389 ms |
94528 KB |
Output is correct |
5 |
Correct |
396 ms |
94748 KB |
Output is correct |
6 |
Correct |
1063 ms |
71816 KB |
Output is correct |
7 |
Correct |
426 ms |
113256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
440 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |