#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int LIM = 1e6;
vector<array<int, 2>> p(LIM), g[LIM];
int r, dfsTimer, t0[LIM], t1[LIM], vis[LIM];
ll best, d[LIM], s[LIM], pre[2*LIM], suf[2*LIM];
void dfs(int u){
s[u] = d[u];
t0[u] = dfsTimer++;
ll curr = 0;
for(auto &[v, w] : g[u]) if(v != r){
d[v] = d[u] + (ll)w; dfs(v);
s[u] = max(s[u], s[v]);
best = max(best, curr + s[v] - d[u]);
curr = max(curr, s[v] - d[u]);
}
t1[u] = dfsTimer++;
pre[t0[u]] = suf[t0[u]] = d[u];
pre[t1[u]] = suf[t1[u]] = 0;
}
void findCycle(int u, int par){
vis[u] = 1;
for(auto &[v, w] : g[u]){
if(vis[v] == 1) r = v;
if(vis[v] == 0) findCycle(v, u);
}
vis[u] = 2;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i=0; i<n; ++i){
cin >> p[i][0] >> p[i][1];
g[--p[i][0]].push_back({i, p[i][1]});
}
ll ans = 0;
for(int z=0; z<n; ++z){
if(vis[z]) continue;
r = -1, best = 0;
findCycle(z, z);
bool cycle = r >= 0;
if(!cycle) r = z;
dfsTimer = 0;
dfs(r);
assert(cycle);
for(int i=1; i<dfsTimer; ++i) pre[i] = max(pre[i], pre[i-1]);
for(int i=dfsTimer-2; i>=0; --i) suf[i] = max(suf[i], suf[i+1]);
int u = p[r][0], last = -1;
ll res = 0;
while(u != r){
ll mx = 0;
for(auto &[v, w] : g[u])
if(v != last) mx = max(mx, s[v] - d[u]);
ll ex = 0;
if(t0[u]) ex = max(ex, pre[t0[u]-1]);
if(t1[u] + 1 < dfsTimer) ex = max(ex, suf[t1[u]+1]);
res = max(res, d[p[r][0]] - d[u] + mx + ex);
last = u;
u = p[u][0];
}
res += p[r][1];
ans += max(best, res);
}
// assert(cnt == 1);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31564 KB |
Output is correct |
2 |
Runtime error |
53 ms |
64004 KB |
Execution killed with signal 6 |
3 |
Correct |
20 ms |
31656 KB |
Output is correct |
4 |
Runtime error |
55 ms |
64044 KB |
Execution killed with signal 6 |
5 |
Correct |
20 ms |
31692 KB |
Output is correct |
6 |
Runtime error |
56 ms |
64024 KB |
Execution killed with signal 6 |
7 |
Runtime error |
55 ms |
64076 KB |
Execution killed with signal 6 |
8 |
Runtime error |
55 ms |
64052 KB |
Execution killed with signal 6 |
9 |
Runtime error |
56 ms |
64068 KB |
Execution killed with signal 6 |
10 |
Runtime error |
54 ms |
63972 KB |
Execution killed with signal 6 |
11 |
Correct |
19 ms |
31672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
54 ms |
63976 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
56 ms |
64020 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
65 ms |
64692 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
72 ms |
65700 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
114 ms |
72004 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
53156 KB |
Output is correct |
2 |
Runtime error |
199 ms |
81324 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
307 ms |
80168 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
407 ms |
131072 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |