/*
Problem: P4381 [IOI2008] Island
When: 2023-11-19 19:41:26
Author: Ning07
*/
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int NN=2e6;
int N, A[NN], deg[NN];
i64 ans, f[NN], g[NN], W[NN];
i64 get(int p){
int st=p; p=A[p];
i64 m1=f[st],m2=f[st];
i64 s=W[st],ans1=g[st],ans2=-1E18;
while (p != st) {
deg[p] = 0;
ans1=max(ans1,max(g[p],f[p]+s+m1));
ans2=max(ans2,f[p]-s+m2);
m1=max(m1,f[p]-s),m2=max(m2,f[p]+s);
s+=W[p],p=A[p];
}
return max(ans1,ans2+s);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>N;
for(int i=1;i<=N;i++){
cin>>A[i]>>W[i];
++deg[A[i]];
}
queue<int> q;
for(int i=1;i<=N;i++)
if (!deg[i])
q.push(i);
while (!q.empty()){
int p=q.front(); q.pop();
i64 c=f[p]+W[p];
g[A[p]]=max(g[A[p]],max(f[A[p]]+c,g[p]));
f[A[p]]=max(f[A[p]],c);
if (!--deg[A[p]]) q.push(A[p]);
}
for(int i=1;i<=N;i++)
if (deg[i])
ans += get(i);
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4456 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4704 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4560 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
6 ms |
7772 KB |
Output is correct |
3 |
Correct |
7 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8284 KB |
Output is correct |
2 |
Correct |
15 ms |
9064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
14720 KB |
Output is correct |
2 |
Correct |
27 ms |
14460 KB |
Output is correct |
3 |
Correct |
39 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
23628 KB |
Output is correct |
2 |
Correct |
72 ms |
27256 KB |
Output is correct |
3 |
Correct |
66 ms |
21844 KB |
Output is correct |
4 |
Correct |
96 ms |
37460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
37128 KB |
Output is correct |
2 |
Correct |
154 ms |
44088 KB |
Output is correct |
3 |
Correct |
113 ms |
42620 KB |
Output is correct |
4 |
Correct |
148 ms |
51188 KB |
Output is correct |
5 |
Correct |
140 ms |
50952 KB |
Output is correct |
6 |
Correct |
263 ms |
52532 KB |
Output is correct |
7 |
Correct |
156 ms |
53408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
52048 KB |
Output is correct |
2 |
Correct |
162 ms |
52080 KB |
Output is correct |
3 |
Correct |
139 ms |
36692 KB |
Output is correct |
4 |
Correct |
135 ms |
36128 KB |
Output is correct |
5 |
Correct |
149 ms |
51356 KB |
Output is correct |
6 |
Correct |
154 ms |
50772 KB |
Output is correct |
7 |
Correct |
202 ms |
52820 KB |
Output is correct |