#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mxn=1000001,INF=1e18;
vector <int> ke[mxn],ve[mxn],s[mxn],a[mxn],b[mxn];
int n,p[mxn],l[mxn],vis[mxn],vis2[mxn],root[mxn],pos[mxn],cycle[mxn],dp[mxn],dp2[mxn],d[mxn],mx[mxn],idx,res;
void dfs(int u){
for (int v:ke[u]){
d[v]=d[u]+l[v];
dfs(v);
dp[u]=max(dp[u],dp[v]+l[v]);
}
}
void dfs2(int u){
pair <int, int> mx={-1,0},mx2={-1,0};
for (int v:ke[u]){
auto tmp=make_pair(dp[v]+l[v],v);
if (tmp>mx){
mx2=mx;
mx=tmp;
}
else
mx2=max(mx2,tmp);
}
for (int v:ke[u]){
dp2[v]=max(dp2[u]+l[v],(mx.second==v?mx2.first:mx.first));
dfs2(v);
}
}
int32_t main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++)
cin >> p[i] >> l[i];
for (int i=1;i<=n;i++)
if (!vis[i]){
vector <int> tmp;
while (true){
tmp.push_back(i);
vis2[i]=tmp.size();
i=p[i];
if (vis[i]){
for (int j:tmp){
root[j]=root[i];
cycle[j]=cycle[i];
ke[p[j]].push_back(j);
vis[j]=1;
}
break;
}
if (vis2[i]){
for (int j=vis2[i]-1;j<tmp.size();j++){
root[tmp[j]]=tmp[j];
cycle[tmp[j]]=idx;
pos[tmp[j]]=j-vis2[i]+1;
ve[idx].push_back(tmp[j]);
s[idx].push_back((s[idx].empty()?0:s[idx].back())+l[tmp[(j>=vis2[i]?j:tmp.size())-1]]);
}
for (int j=0;j<vis2[i]-1;j++){
root[tmp[j]]=i;
ke[tmp[j+1]].push_back(tmp[j]);
}
for (int j:tmp){
cycle[j]=cycle[i];
vis[j]=1;
}
idx++;
break;
}
}
}
for (int i=1;i<=n;i++)
if (root[i]==i){
dfs(i);
dfs2(i);
}
for (int i=0;i<idx;i++){
int sz=s[i].size();
a[i].assign(sz,0);
b[i].assign(sz,0);
a[i].push_back(-INF);
b[i].push_back(-INF);
for (int j=s[i].size()-1;j>=0;j--){
a[i][j]=max(a[i][j+1],dp[ve[i][j]]+s[i][j]);
b[i][j]=max(b[i][j+1],dp[ve[i][j]]-s[i][j]);
}
}
for (int i=1;i<=n;i++){
int j=cycle[i],k=pos[root[i]],val=max(max(dp[i],dp2[i]),d[i]+max(a[j][k+1]-s[j][k],b[j][k+1]+s[j].back()+s[j][k]));
mx[j]=max(mx[j],val);
}
for (int i=0;i<idx;i++)
res+=mx[i];
cout << res;
}
Compilation message
islands.cpp: In function 'int32_t main()':
islands.cpp:52:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int j=vis2[i]-1;j<tmp.size();j++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
129804 KB |
Output is correct |
2 |
Incorrect |
27 ms |
129884 KB |
Output isn't correct |
3 |
Correct |
28 ms |
129928 KB |
Output is correct |
4 |
Correct |
27 ms |
129884 KB |
Output is correct |
5 |
Correct |
27 ms |
129876 KB |
Output is correct |
6 |
Incorrect |
27 ms |
129880 KB |
Output isn't correct |
7 |
Incorrect |
28 ms |
129780 KB |
Output isn't correct |
8 |
Incorrect |
27 ms |
129884 KB |
Output isn't correct |
9 |
Incorrect |
27 ms |
129740 KB |
Output isn't correct |
10 |
Incorrect |
29 ms |
129876 KB |
Output isn't correct |
11 |
Correct |
27 ms |
129992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
129876 KB |
Output is correct |
2 |
Correct |
27 ms |
129872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
130136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
131016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
31 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
48 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
78 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
122 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
126 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |