#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 1e6 + 11;
// 40
vector<pair<int, int>> G[N];
int p[N], w[N], tin[N], t = 0;
long long dp[N];
bool vis[N], cyc[N];
// 12
struct edge{
int u, v, w;
};
vector<edge> extra;
void dfs(int id, int v, int pa = -1){
vis[v] = true; tin[v] = ++t;
for(auto i : G[v]){
if(!vis[i.fi]){
p[i.fi] = v;
dfs(id, i.fi, v);
w[i.fi] = i.se;
}else if(i.fi != pa){
extra[id] = {v, i.fi, i.se};
}
}
}
bool is_anc(int u, int v){
return tin[u] <= tin[v];
}
long long dfs_dp(int id, int v, int pa = -1){
// 8
vector<long long> dis;
long long ans = 0;
for(auto i : G[v]){
if(!cyc[i.fi] && i.fi != pa){
long long r = dfs_dp(id, i.fi, v);
ans = max(ans, r);
dis.push_back(dp[i.fi] + i.se);
}
}
sort(dis.begin(), dis.end());
if(dis.size() >= 2) ans = max(ans, dis.back() + dis[dis.size() - 2]);
if(dis.size()) dp[v] = dis.back(), ans = max(ans, dis.back());
return ans;
}
// 32
struct SegTree{
long long t[1 << 22];
void upd(int pos, long long val, int v, int l, int r){
if(l == r) return void(t[v] = val);
int m = (l + r) / 2;
if(pos <= m){
upd(pos, val, v * 2 + 1, l, m);
}else{
upd(pos, val, v * 2 + 2, m + 1, r);
}
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
long long qry(int ql, int qr, int v, int l, int r){
if(qr < l || r < ql) return 0;
if(ql <= l && r <= qr) return t[v];
int m = (l + r) / 2;
long long qa = qry(ql, qr, v * 2 + 1, l, m);
long long qb = qry(ql, qr, v * 2 + 2, m + 1, r);
return max(qa, qb);
}
} segTree;
long long solve(int id, vector<long long>& DP, vector<int>& W){
int n = W.size();
// 16
vector<long long> S(n * 2 + 1);
S[0] = 0;
for(int i = 0; i < n; i++){
S[i + 1] = S[i] + W[i];
}
for(int i = 0; i < n; i++){
S[n + i + 1] = S[n + i] + W[i];
}
for(int i = 0; i < n * 2; i++){
segTree.upd(i, S[i] + DP[i % n], 0, 0, n * 2 - 1);
}
long long ans = 0;
for(int i = 0; i < n; i++){
ans = max(ans, DP[i] - S[i] + segTree.qry(i + 1, i + n - 1, 0, 0, n * 2 - 1));
}
return ans;
}
int32_t main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
int n; cin >> n;
for(int i = 1; i <= n; i++){
int v, _w; cin >> v >> _w;
G[i].push_back({v, _w});
G[v].push_back({i, _w});
}
for(int i = 1; i <= n; i++){
G[i].shrink_to_fit();
}
// 4
vector<int> roots;
for(int i = 1; i <= n; i++){
if(!vis[i]){
extra.resize(extra.size() + 1);
dfs(roots.size(), i);
roots.push_back(i);
}
}
// 20
int CC = roots.size();
long long ans = 0;
for(int i = 0; i < CC; i++){
vector<int> cyc_L, cyc_R;
int x = extra[i].u, y = extra[i].v;
while(!is_anc(x, y)){
cyc[x] = true;
cyc_L.push_back(x);
x = p[x];
}
while(!is_anc(y, x)){
cyc[y] = true;
cyc_R.push_back(y);
y = p[y];
}
int l = x;
cyc[l] = true;
long long rans = 0;
for(auto v : cyc_L){
rans = max(rans, dfs_dp(i, v));
}
for(auto v : cyc_R){
rans = max(rans, dfs_dp(i, v));
}
rans = max(rans, dfs_dp(i, l));
vector<int> W; vector<long long> DP;
for(auto i : cyc_L){
DP.push_back(dp[i]);
W.push_back(w[i]);
}
DP.push_back(dp[l]);
for(int i = (int) cyc_R.size() - 1; i >= 0; i--){
DP.push_back(dp[cyc_R[i]]);
W.push_back(w[cyc_R[i]]);
}
cyc_L.resize(0); cyc_R.resize(0);
W.push_back(extra[i].w);
rans = max(rans, solve(i, DP, W));
ans += rans;
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23780 KB |
Output is correct |
3 |
Correct |
12 ms |
23876 KB |
Output is correct |
4 |
Correct |
12 ms |
23768 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23824 KB |
Output is correct |
7 |
Correct |
13 ms |
23788 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23768 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23904 KB |
Output is correct |
2 |
Correct |
13 ms |
23908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23892 KB |
Output is correct |
2 |
Correct |
15 ms |
24304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25104 KB |
Output is correct |
2 |
Correct |
31 ms |
28212 KB |
Output is correct |
3 |
Correct |
23 ms |
25132 KB |
Output is correct |
4 |
Correct |
18 ms |
24428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
30168 KB |
Output is correct |
2 |
Correct |
58 ms |
32404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
38968 KB |
Output is correct |
2 |
Correct |
133 ms |
48116 KB |
Output is correct |
3 |
Correct |
176 ms |
64468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
48280 KB |
Output is correct |
2 |
Correct |
247 ms |
84768 KB |
Output is correct |
3 |
Correct |
367 ms |
89332 KB |
Output is correct |
4 |
Correct |
411 ms |
122580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
91736 KB |
Output is correct |
2 |
Runtime error |
689 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
304 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |