#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6+5;
struct Edge{
int i,x;
long long d;
Edge(){};
Edge(int a, int b, long long c){i=a;x=b;d=c;};
};
int N;
long long D[MAX],Ans,Len;
bool vE[MAX],vN[MAX],isL[MAX];
vector<Edge> I[MAX];
stack<int> L;
vector<int> Lp;
long long A[MAX],B[MAX],C[MAX],dis[MAX];
void DFS(int i)
{
if(vN[i]){
Lp.push_back(i);
isL[i] = true;
while(L.top()!=i){
Lp.push_back(L.top());
isL[L.top()] = true;
L.pop();
}
return;
}
vN[i] = true;
L.push(i);
for(auto it : I[i]){
if(vE[it.i]) continue;
vE[it.i] = true;
DFS(it.x);
}
if(!L.empty()) L.pop();
}
long long getD(int x, long long d, int r)
{
long long res=0,tmp=0,subm=0;
for(auto it : I[x]){
if(isL[it.x] || it.x==r) continue;
tmp = getD(it.x,it.d,x);
if(res<=tmp){
subm = res;
res = tmp;
}
else subm = max(subm,tmp);
Len = max(Len,subm+res);
}
return res+d;
}
long long getL()
{
long long res=0,tmp;
int n = Lp.size();
Len = 0;
for(int i=0; i<n; i++)
{
for(auto it : I[Lp[i]]){
if(it.x==Lp[(i+1)%n]) dis[i] = it.d;
}
D[Lp[i]] = getD(Lp[i],0,Lp[i]);
}
res = Len;
A[0] = D[Lp[0]];
for(int i=1; i<n; i++) A[i] = A[i-1]+D[Lp[i]]-D[Lp[i-1]]+dis[i-1];
for(int i=0; i<n; i++) B[i] = A[i]-D[Lp[i]]*2;
C[0] = D[Lp[n-1]]+dis[n-1];
for(int i=1; i<n-1; i++) C[i] = C[i-1]+D[Lp[n-1-i]]-D[Lp[n-i]]+dis[n-1-i];
tmp = B[0];
res = max(res,A[1] - B[0]);
for(int i=2; i<n; i++){
tmp = min(tmp,B[i-1]);
res = max(res,A[i] - tmp);
}
tmp = A[0];
res = max(res,C[n-2] + A[0]);
for(int i=n-3; i>=0; i--){
tmp = max(tmp,A[n-2-i]);
res = max(res,C[i] + tmp);
}
return res;
}
int main()
{
cin >> N;
for(int i=1; i<=N; i++){
int x;
long long d;
cin >> x >> d;
I[i].push_back(Edge(i,x,d));
I[x].push_back(Edge(i,i,d));
}
for(int j=1; j<=N; j++){
if(vN[j]) continue;
while(!Lp.empty()) Lp.pop_back();
DFS(j);
Ans += getL();
}
cout << Ans;
}
/*8
3 8
7 2
4 2
1 4
1 17
3 4
2 3
1 15
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23928 KB |
Output is correct |
2 |
Correct |
25 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23928 KB |
Output is correct |
4 |
Correct |
19 ms |
23928 KB |
Output is correct |
5 |
Correct |
18 ms |
23800 KB |
Output is correct |
6 |
Incorrect |
19 ms |
23800 KB |
Output isn't correct |
7 |
Incorrect |
19 ms |
23864 KB |
Output isn't correct |
8 |
Correct |
19 ms |
23928 KB |
Output is correct |
9 |
Incorrect |
18 ms |
23928 KB |
Output isn't correct |
10 |
Correct |
18 ms |
23928 KB |
Output is correct |
11 |
Incorrect |
19 ms |
23928 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23928 KB |
Output is correct |
2 |
Incorrect |
21 ms |
23972 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
23928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
24952 KB |
Output is correct |
2 |
Correct |
52 ms |
27552 KB |
Output is correct |
3 |
Correct |
41 ms |
25324 KB |
Output is correct |
4 |
Correct |
29 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
28768 KB |
Output is correct |
2 |
Correct |
99 ms |
31696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
37956 KB |
Output is correct |
2 |
Correct |
190 ms |
43132 KB |
Output is correct |
3 |
Correct |
260 ms |
52908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
360 ms |
49760 KB |
Output is correct |
2 |
Correct |
447 ms |
72608 KB |
Output is correct |
3 |
Correct |
454 ms |
80560 KB |
Output is correct |
4 |
Correct |
635 ms |
97908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
819 ms |
72812 KB |
Output is correct |
2 |
Runtime error |
1510 ms |
132096 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
986 ms |
132096 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |