#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e6;
vector<int> e[N+10];
int f[N+10];
int l[N+10];
bitset<N+10> vis_fill;
bitset<N+10> vis_s;
bitset<N+10> on_cycle;
vector<int> t;
void dfs_fill(int x)
{
t.push_back(x);
vis_fill[x]=true;
for(auto v:e[x])
{
if(!vis_fill[v])
dfs_fill(v);
}
return;
}
pair<long long,long long> dfs_tree(int x)
{
on_cycle[x]=true;
pair<long long,long long> ans={0,0};
for(auto v:e[x])
{
if(on_cycle[v])
continue;
auto tmp=dfs_tree(v);
ans.fi=max({ans.fi,tmp.fi,ans.se+tmp.se+(v==f[x] ? l[x]:l[v])});
ans.se=max(ans.se,tmp.se+(v==f[x] ? l[x]:l[v]));
}
ans.fi=max(ans.fi,ans.se);
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++)
{
cin>>f[i]>>l[i];
e[i].push_back(f[i]);
e[f[i]].push_back(i);
}
long long ans=0;
for(int j=1;j<=n;j++)
{
if(!vis_fill[j])
{
t.clear();
dfs_fill(j);
long long ans_s=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);
ans_s=max(ans_s,ww.fi);
ans_s=max({ans_s,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);
ans+=ans_s;
}
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
13 ms |
23732 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
13 ms |
23744 KB |
Output is correct |
7 |
Correct |
13 ms |
23748 KB |
Output is correct |
8 |
Correct |
13 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
13 ms |
23804 KB |
Output is correct |
11 |
Correct |
14 ms |
23804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23824 KB |
Output is correct |
2 |
Correct |
14 ms |
23812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23944 KB |
Output is correct |
2 |
Correct |
15 ms |
24140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
24772 KB |
Output is correct |
2 |
Correct |
30 ms |
26820 KB |
Output is correct |
3 |
Correct |
24 ms |
25088 KB |
Output is correct |
4 |
Correct |
20 ms |
24456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
27936 KB |
Output is correct |
2 |
Correct |
48 ms |
29856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
34908 KB |
Output is correct |
2 |
Correct |
96 ms |
38912 KB |
Output is correct |
3 |
Correct |
122 ms |
44940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
41604 KB |
Output is correct |
2 |
Correct |
198 ms |
58940 KB |
Output is correct |
3 |
Correct |
219 ms |
66240 KB |
Output is correct |
4 |
Correct |
266 ms |
78124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
73808 KB |
Output is correct |
2 |
Correct |
777 ms |
100444 KB |
Output is correct |
3 |
Correct |
290 ms |
57484 KB |
Output is correct |
4 |
Correct |
384 ms |
85228 KB |
Output is correct |
5 |
Correct |
378 ms |
86984 KB |
Output is correct |
6 |
Correct |
1036 ms |
66912 KB |
Output is correct |
7 |
Correct |
415 ms |
105456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
419 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |