#include <bits/stdc++.h>
using namespace std;
const int mxn=1000001;
const long long INF=1e18;
vector <int> ke[mxn],ve[mxn],V[mxn],tmp;
vector <long long> s;
int n,p[mxn],l[mxn],vis[mxn],vis2[mxn],root[mxn],cycle[mxn],idx;
long long dp[mxn],dp2[mxn],d[mxn],mx,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 <long long, 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],(mx.second==v?mx2.first:mx.first))+l[v];
dfs2(v);
}
}
int 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]){
int j=i;
tmp.clear();
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;
ve[idx].push_back(tmp[j]);
}
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;
}
}
i=j;
}
for (int i=1;i<=n;i++)
if (root[i]==i){
dfs(i);
dfs2(i);
}
for (int i=1;i<=n;i++)
V[root[i]].push_back(i);
for (int i=0;i<idx;i++){
int sz=ve[i].size();
long long a=-INF,b=-INF;
for (int j=0;j<sz;j++)
s.push_back((s.empty()?0:s.back())+l[ve[i][(j?j:sz)-1]]);
mx=0;
for (int j=sz-1;j>=0;j--){
for (int u:V[ve[i][j]]){
long long val=max(max(dp[u],dp2[u]),d[u]+max(a-s[j],b+s.back()+s[j]));
mx=max(mx,val);
}
a=max(a,dp[ve[i][j]]+s[j]);
b=max(b,dp[ve[i][j]]-s[j]);
}
s.clear();
res+=mx;
}
cout << res;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:55:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int j=vis2[i]-1;j<tmp.size();j++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
80732 KB |
Output is correct |
2 |
Correct |
17 ms |
80732 KB |
Output is correct |
3 |
Correct |
17 ms |
80732 KB |
Output is correct |
4 |
Correct |
17 ms |
80728 KB |
Output is correct |
5 |
Correct |
19 ms |
81232 KB |
Output is correct |
6 |
Correct |
17 ms |
80984 KB |
Output is correct |
7 |
Correct |
17 ms |
80732 KB |
Output is correct |
8 |
Correct |
19 ms |
80728 KB |
Output is correct |
9 |
Correct |
17 ms |
80732 KB |
Output is correct |
10 |
Correct |
17 ms |
80732 KB |
Output is correct |
11 |
Correct |
18 ms |
80732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
80732 KB |
Output is correct |
2 |
Correct |
17 ms |
80728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
80704 KB |
Output is correct |
2 |
Correct |
19 ms |
81044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
83804 KB |
Output is correct |
2 |
Correct |
28 ms |
87132 KB |
Output is correct |
3 |
Correct |
23 ms |
84040 KB |
Output is correct |
4 |
Correct |
21 ms |
83448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
88156 KB |
Output is correct |
2 |
Correct |
39 ms |
90936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
98104 KB |
Output is correct |
2 |
Correct |
60 ms |
99160 KB |
Output is correct |
3 |
Correct |
78 ms |
104664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
115576 KB |
Output is correct |
2 |
Correct |
127 ms |
128728 KB |
Output is correct |
3 |
Correct |
123 ms |
124240 KB |
Output is correct |
4 |
Runtime error |
159 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
189 ms |
131072 KB |
Output is correct |
2 |
Runtime error |
351 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
201 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |