#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int LIM = 1e6;
vector<array<int, 2>> a(LIM), g[LIM];
int r, p[LIM], dfsTimer, t0[LIM], t1[LIM];
ll best, d[LIM], s[LIM], pre[2*LIM], suf[2*LIM];
bitset<LIM> vis;
void dfs(int u, int par){
p[u] = par, s[u] = -1;
t0[u] = dfsTimer++;
ll curr = 0;
for(auto &[v, w] : g[u]) if(v != par && v != r){
d[v] = d[u] + (ll)w, dfs(v, u);
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]] = 0;
pre[t1[u]] = suf[t1[u]] = 0;
if(t0[u] + 1 == t1[u]) pre[t0[u]] = suf[t0[u]] = s[u] = d[u];
}
void findCycle(int u, int par){
vis[u] = 1;
for(auto &[v, w] : g[u]) if(v != par){
if(vis[v]) return void(r = v);
findCycle(v, u);
if(r >= 0) return;
}
vis[u] = 0;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i=0; i<n; ++i){
cin >> a[i][0] >> a[i][1];
g[--a[i][0]].push_back({i, a[i][1]});
}
fill(p, p+n, -1);
ll ans = 0;
for(int z=0; z<n; ++z){
if(p[z] >= 0) continue;
r = -1;
best = 0;
findCycle(z, z);
bool cycle = r >= 0;
if(!cycle) r = z;
dfsTimer = 0;
dfs(r, r);
if(!cycle){
ans += best;
continue;
}
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 = a[r][0], last = -1;
ll res = 0, h = 0;
while(u != r){
ll mx = 0;
for(auto &[v, w] : g[u]){
if(v == last) h += w;
else mx = max(mx, s[v] - d[u]);
}
ll ex = t0[u] ? pre[t0[u]-1] : 0LL;
ex += t1[u]+1 < dfsTimer ? suf[t1[u]+1] : 0LL;
res = max(res, h + mx + ex);
last = u;
u = p[u];
}
res = max(res, h);
res += a[r][1];
ans += max(best, res);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
31692 KB |
Output is correct |
2 |
Incorrect |
20 ms |
31620 KB |
Output isn't correct |
3 |
Incorrect |
20 ms |
31696 KB |
Output isn't correct |
4 |
Correct |
20 ms |
31668 KB |
Output is correct |
5 |
Correct |
24 ms |
31692 KB |
Output is correct |
6 |
Incorrect |
19 ms |
31640 KB |
Output isn't correct |
7 |
Incorrect |
20 ms |
31692 KB |
Output isn't correct |
8 |
Incorrect |
18 ms |
31688 KB |
Output isn't correct |
9 |
Incorrect |
19 ms |
31668 KB |
Output isn't correct |
10 |
Correct |
18 ms |
31640 KB |
Output is correct |
11 |
Incorrect |
18 ms |
31608 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
31692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
31796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
32792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
36924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
128 ms |
48892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
58692 KB |
Output is correct |
2 |
Incorrect |
228 ms |
90900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
557 ms |
98408 KB |
Output is correct |
2 |
Runtime error |
783 ms |
131076 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
415 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |