#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int N = 1e6 + 11;
vector<pair<int, int>> G[N];
int p[N], w[N], de[N], anc[N][20], dp[N];
bool vis[N], cyc[N];
struct edge{
int u, v, w;
};
vector<edge> extra;
void dfs(int id, int v, int pa = -1){
vis[v] = true;
for(auto i : G[v]){
if(!vis[i.fi]){
de[i.fi] = de[v] + 1;
p[i.fi] = v; anc[i.fi][0] = p[i.fi];
for(int j = 1; j < 20; j++) anc[i.fi][j] = anc[anc[i.fi][j - 1]][j - 1];
dfs(id, i.fi, v);
w[i.fi] = i.se;
}else if(i.fi != pa){
extra[id] = {v, i.fi, i.se};
}
}
}
int kth_anc(int u, int k){
for(int j = 19; j >= 0; j--){
if(k & (1 << j)) u = anc[u][j];
}
return u;
}
int lca(int u, int v){
if(de[u] < de[v]) swap(u, v);
u = kth_anc(u, de[u] - de[v]);
for(int i = 19; i >= 0; i--){
if(anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
}
return u == v ? u : p[u];
}
int ans[N];
void dfs_dp(int id, int v, int pa = -1){
vector<int> dis;
for(auto i : G[v]){
if(!cyc[i.fi] && i.fi != pa){
dfs_dp(id, i.fi, v);
dis.push_back(dp[i.fi] + i.se);
}
}
sort(dis.begin(), dis.end());
if(dis.size() >= 2) ans[id] = max(ans[id], dis.back() + dis[dis.size() - 2]);
if(dis.size()) dp[v] = dis.back(), ans[id] = max(ans[id], dis.back());
}
void solve(int id, vector<int> DP, vector<int> W){
int n = W.size();
vector<int> S(n * 2 + 1), B(n * 2);
int st[n * 2][20];
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; i++){
DP.push_back(DP[i]);
}
for(int i = 0; i < n * 2; i++){
st[i][0] = B[i] = DP[i] + S[i];
}
for(int j = 1; j < 20; j++){
for(int i = 0; i <= n * 2 - (1 << j); i++){
st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}
}
for(int i = 0; i < n; i++){
int k = __lg(n);
int w = max(st[i][k], st[i + n - (1 << k)][k]);
ans[id] = max(ans[id], DP[i] - S[i] + w);
}
}
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});
}
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);
}
}
int CC = roots.size();
for(int i = 0; i < CC; i++){
vector<pair<int, int>> cyc_L, cyc_R;
int x = extra[i].u, y = extra[i].v;
int l = lca(x, y);
vector<int> cyc_v;
while(x != l){
cyc_v.push_back(x);
cyc[x] = true;
cyc_L.push_back({x, w[x]});
x = p[x];
}
while(y != l){
cyc_v.push_back(y);
cyc[y] = true;
cyc_R.push_back({y, w[y]});
y = p[y];
}
cyc_v.push_back(l);
cyc[l] = true;
for(auto v : cyc_v){
dfs_dp(i, v);
}
vector<int> V, W, DP;
for(auto i : cyc_L){
V.push_back(i.fi);
DP.push_back(dp[i.fi]);
W.push_back(i.se);
}
V.push_back(l);
DP.push_back(dp[l]);
for(int i = (int) cyc_R.size() - 1; i >= 0; i--){
V.push_back(cyc_R[i].fi);
DP.push_back(dp[cyc_R[i].fi]);
W.push_back(cyc_R[i].se);
}
W.push_back(extra[i].w);
solve(i, DP, W);
}
cout << accumulate(ans, ans + CC, 0LL) << endl;
}
Compilation message
islands.cpp: In function 'void solve(long long int, std::vector<long long int>, std::vector<long long int>)':
islands.cpp:83:48: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
83 | st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
12 ms |
23812 KB |
Output isn't correct |
3 |
Correct |
12 ms |
24020 KB |
Output is correct |
4 |
Correct |
12 ms |
23748 KB |
Output is correct |
5 |
Correct |
12 ms |
23836 KB |
Output is correct |
6 |
Correct |
12 ms |
23780 KB |
Output is correct |
7 |
Correct |
14 ms |
23816 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Incorrect |
12 ms |
23892 KB |
Output isn't correct |
11 |
Correct |
12 ms |
23816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24276 KB |
Output is correct |
2 |
Correct |
15 ms |
24260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24276 KB |
Output is correct |
2 |
Correct |
15 ms |
25672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
28484 KB |
Output is correct |
2 |
Correct |
54 ms |
38732 KB |
Output is correct |
3 |
Incorrect |
26 ms |
29008 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
44612 KB |
Output is correct |
2 |
Correct |
66 ms |
55352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
80628 KB |
Output is correct |
2 |
Correct |
191 ms |
110188 KB |
Output is correct |
3 |
Runtime error |
199 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
125088 KB |
Output is correct |
2 |
Runtime error |
220 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
312 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
334 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |