#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;
}
long long solve()
{
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);
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);
e[a].emplace_back(i);
}
long long ans=0;
for(int i=1;i<=n;i++)
{
if(!vis_fill[i])
{
t.clear();
dfs_fill(i);
ans+=solve();
}
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23796 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23884 KB |
Output is correct |
6 |
Correct |
14 ms |
23760 KB |
Output is correct |
7 |
Correct |
15 ms |
23756 KB |
Output is correct |
8 |
Correct |
15 ms |
23756 KB |
Output is correct |
9 |
Correct |
13 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
14 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23884 KB |
Output is correct |
2 |
Correct |
14 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23820 KB |
Output is correct |
2 |
Correct |
15 ms |
24140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
24508 KB |
Output is correct |
2 |
Correct |
30 ms |
26428 KB |
Output is correct |
3 |
Correct |
25 ms |
24808 KB |
Output is correct |
4 |
Correct |
19 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
27344 KB |
Output is correct |
2 |
Correct |
52 ms |
28996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
34224 KB |
Output is correct |
2 |
Correct |
96 ms |
38196 KB |
Output is correct |
3 |
Correct |
123 ms |
44188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
40872 KB |
Output is correct |
2 |
Correct |
196 ms |
58168 KB |
Output is correct |
3 |
Correct |
219 ms |
65528 KB |
Output is correct |
4 |
Correct |
264 ms |
77368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
73032 KB |
Output is correct |
2 |
Correct |
773 ms |
99152 KB |
Output is correct |
3 |
Correct |
305 ms |
56804 KB |
Output is correct |
4 |
Correct |
375 ms |
84416 KB |
Output is correct |
5 |
Correct |
390 ms |
86200 KB |
Output is correct |
6 |
Correct |
1052 ms |
66140 KB |
Output is correct |
7 |
Correct |
407 ms |
104272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
431 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |