#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sub(u) d(t[u], t[u] + s[u] - 1)
#define dis(u) d(t[u], t[u])
#define w (a[e][1])
#define v() (e == u ? a[e][0] : e)
const int LIM = 1e6;
template<class T> struct SegmentTree{
int n = LIM, i; T a[2*LIM] ;
SegmentTree& operator[](int j){ i=j+n; return *this; }
void operator=(T v){
for(a[i]=v; i/=2; ) a[i] = max(a[2*i], a[2*i+1]); }
T operator()(int l, int r){
T x = 0;
for(l+=n, r+=n+1; l<r; l/=2, r/=2){
if(l & 1) x = max(x, a[l++]);
if(r & 1) x = max(x, a[--r]);
}
return x;
}
};
vector<int> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM], dfsTimer, t[LIM], s[LIM];
ll best, lastDist;
bitset<LIM> vis;
SegmentTree<ll> d;
void dfs(int u){
s[u] = 1;
ll curr = 0;
t[u] = dfsTimer++;
d[t[u]] = (u == r ? 0LL : lastDist);
for(int e : g[u]) if(e != r && !s[v()]){
lastDist = dis(u) + (ll)w, p[v()] = u;
dfs(v());
s[u] += s[v()];
best = max(best, curr + sub(v()) - dis(u));
curr = max(curr, sub(v()) - dis(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);
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);
g[i].push_back(i);
}
fill(p, p+n, -1);
ll ans = 0;
for(int z=0; z<n; ++z){
if(s[z]) continue;
r = -1, best = 0;
findCycle(z, -1);
bool cycle = r >= 0;
if(!cycle) r = z;
dfsTimer = 0;
dfs(r);
if(!cycle){
ans += best;
continue;
}
assert(cycle);
int u = a[r][0], last = -1;
ll res = 0, ex;
while(u != r){
ll mx = 0;
for(int e : g[u])
if(e != r && v() != p[u] && v() != last) mx = max(mx, sub(v()) - dis(u));
ex = max(d(0, t[u]-1), d(t[u]+s[u], dfsTimer-1));
res = max(res, dis(a[r][0]) - dis(u) + mx + ex);
last = u;
u = p[u];
}
res += a[r][1];
ans += max(best, res);
for(int i=0; i<dfsTimer; ++i) d[i] = 0;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23884 KB |
Output is correct |
2 |
Correct |
14 ms |
23884 KB |
Output is correct |
3 |
Correct |
15 ms |
23968 KB |
Output is correct |
4 |
Correct |
14 ms |
23828 KB |
Output is correct |
5 |
Correct |
16 ms |
23884 KB |
Output is correct |
6 |
Correct |
15 ms |
23884 KB |
Output is correct |
7 |
Correct |
14 ms |
23884 KB |
Output is correct |
8 |
Correct |
15 ms |
23892 KB |
Output is correct |
9 |
Correct |
16 ms |
23844 KB |
Output is correct |
10 |
Correct |
16 ms |
23884 KB |
Output is correct |
11 |
Correct |
15 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23972 KB |
Output is correct |
2 |
Correct |
15 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23884 KB |
Output is correct |
2 |
Correct |
17 ms |
24268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24900 KB |
Output is correct |
2 |
Correct |
45 ms |
27764 KB |
Output is correct |
3 |
Correct |
30 ms |
25208 KB |
Output is correct |
4 |
Correct |
22 ms |
24492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
29292 KB |
Output is correct |
2 |
Correct |
77 ms |
31220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
39456 KB |
Output is correct |
2 |
Correct |
219 ms |
45628 KB |
Output is correct |
3 |
Correct |
243 ms |
56980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
46400 KB |
Output is correct |
2 |
Correct |
398 ms |
79292 KB |
Output is correct |
3 |
Correct |
496 ms |
87748 KB |
Output is correct |
4 |
Correct |
532 ms |
108900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
545 ms |
94312 KB |
Output is correct |
2 |
Runtime error |
718 ms |
131076 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
399 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |