#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define w (a[e][1])
#define V() (e == u ? a[e][0] : e)
#define v V()
#define E u
const int LIM = 1e6;
vector<int> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM];
ll d[LIM], s[LIM], best;
bitset<LIM> vis;
void dfs(int u){
vis[u] = 1, s[u] = d[u];
ll curr = 0;
for(int e : g[u]) if(e != r && !vis[v]){
p[v] = u;
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]);
}
if(u != r && !vis[a[u][0]]){
p[a[u][0]] = u;
d[a[u][0]] = d[u] + (ll)a[u][1];
dfs(a[u][0]);
s[u] = max(s[u], s[a[u][0]]);
best = max(best, curr + s[a[u][0]] - d[u]);
curr = max(curr, s[a[u][0]] - d[u]);
}
}
void findCycle(int u, int par){
vis[u] = 1;
for(int e : g[u]) if(e != par){
if(vis[v]) return void(r = e + (vis[u] = 0));
findCycle(v, e);
if(r >= 0) break;
}
if(u != par){
if(vis[a[u][0]]) return void(r = u + (vis[u] = 0));
findCycle(a[u][0], u);
}
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);
// g[i].push_back(i);
}
fill(p, p+n, -1);
ll ans = 0;
for(int z=0; z<n; ++z){
if(vis[z]) continue;
r = -1, best = 0;
findCycle(z, -1);
assert(r >= 0);
dfs(r);
int u = a[r][0], last = -1;
while(1){
vis[u] = 0;
s[u] = d[u];
for(int e : g[u])
if(e != r && v != p[u] && v != last) s[u] = max(s[u], s[v]);
if(u != r && a[u][0] != p[u] && a[u][0] != last) s[u] = max(s[u], s[a[u][0]]);
if(u == r) break;
last = u;
u = p[u];
}
u = r;
while(1){
vis[u] = 1;
for(int e : g[u])
if(e != r && v != p[u] && !vis[v]) last = v;
if(u != r && a[u][0] != p[u] && !vis[a[u][0]]) last = a[u][0];
s[last] = max(s[last], s[u]);
if(u == a[r][0]) break;
u = last;
last = -1;
}
u = a[r][0], last = -1;
ll res = 0;
while(u != r){
ll mx = 0;
for(int e : g[u])
if(e != r && v != p[u] && v != last) mx = max(mx, s[v] - d[u]);
if(a[u][0] != p[u] && a[u][0] != last) mx = max(mx, s[a[u][0]] - d[u]);
res = max(res, d[a[r][0]] - d[u] + mx + s[p[u]]);
last = u;
u = p[u];
}
res += a[r][1];
ans += max(best, res);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23768 KB |
Output is correct |
3 |
Correct |
15 ms |
23884 KB |
Output is correct |
4 |
Correct |
15 ms |
23724 KB |
Output is correct |
5 |
Correct |
17 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23824 KB |
Output is correct |
7 |
Correct |
17 ms |
23736 KB |
Output is correct |
8 |
Correct |
14 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23804 KB |
Output is correct |
2 |
Correct |
15 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23880 KB |
Output is correct |
2 |
Correct |
16 ms |
24140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24716 KB |
Output is correct |
2 |
Correct |
31 ms |
26496 KB |
Output is correct |
3 |
Correct |
25 ms |
24784 KB |
Output is correct |
4 |
Correct |
19 ms |
24268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
27412 KB |
Output is correct |
2 |
Correct |
51 ms |
30020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
34348 KB |
Output is correct |
2 |
Correct |
111 ms |
42776 KB |
Output is correct |
3 |
Correct |
143 ms |
47052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
44612 KB |
Output is correct |
2 |
Correct |
235 ms |
61048 KB |
Output is correct |
3 |
Correct |
278 ms |
79088 KB |
Output is correct |
4 |
Correct |
292 ms |
83924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
55316 KB |
Output is correct |
2 |
Correct |
990 ms |
106524 KB |
Output is correct |
3 |
Correct |
308 ms |
61764 KB |
Output is correct |
4 |
Correct |
398 ms |
92664 KB |
Output is correct |
5 |
Correct |
404 ms |
90360 KB |
Output is correct |
6 |
Correct |
1057 ms |
69568 KB |
Output is correct |
7 |
Correct |
467 ms |
128764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
380 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |