#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int LIM = 1e6;
vector<array<int, 3>> g[LIM];
array<int, 2> a[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){
s[u] = d[u];
t0[u] = dfsTimer++;
ll curr = 0;
for(auto &[v, w, e] : g[u]) if(e != par && v != r){
d[v] = d[u] + (ll)w, p[v] = u;
dfs(v, e);
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, e] : g[u]) if(e != par){
if(vis[v]) return void(r = v);
findCycle(v, e);
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]; --a[i][0];
g[a[i][0]].push_back({i, a[i][1], i});
g[i].push_back({a[i][0], a[i][1], i});
}
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;
}
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 = a[r][0], last = -1;
ll res = 0;
while(u != r){
ll mx = 0;
for(auto &[v, w, e] : g[u])
if(v != p[u] && 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[a[r][0]] - d[u] + mx + ex);
last = u;
u = p[u];
}
res += a[r][1];
ans += max(best, res);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Incorrect |
15 ms |
23884 KB |
Output isn't correct |
3 |
Correct |
15 ms |
23940 KB |
Output is correct |
4 |
Correct |
18 ms |
23760 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
16 ms |
23772 KB |
Output is correct |
7 |
Correct |
16 ms |
23804 KB |
Output is correct |
8 |
Incorrect |
15 ms |
23756 KB |
Output isn't correct |
9 |
Correct |
15 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23792 KB |
Output is correct |
11 |
Correct |
17 ms |
23872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24012 KB |
Output is correct |
2 |
Correct |
15 ms |
23952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
24012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
25224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
30916 KB |
Output is correct |
2 |
Correct |
58 ms |
33348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
47020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
55708 KB |
Output is correct |
2 |
Incorrect |
271 ms |
94896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
405 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
433 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |