#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define N 1000002
using namespace std;
int n,l[N],k;
bool fix[N],c[N];
long long len;
long long mx[N],ans,pas;
vector<pair<int,int> >v[N];
int g[N],dis[N],szg,szdis,szd,szc;
void findc(int x,int par){
if(fix[x]){
g[szg] = x;
dis[szdis] = l[par];
szg++;
szdis++;
k = 1;
return;
}
g[szg] = (x);
szg++;
fix[x] = 1;
for(int i=0; i<v[x].size(); i++)
if(v[x][i].s != par){
dis[szdis] = (l[v[x][i].s]);szdis++;
findc(v[x][i].f , v[x][i].s);
if(k)return;
szdis--;
}
szg--;
}
void findmx(int x,int par){
fix[x] = 1;
for(int i=0; i<v[x].size(); i++){
if(v[x][i].s != par && !c[v[x][i].f]){
findmx(v[x][i].f , v[x][i].s);
ans = max(ans , mx[x] + mx[v[x][i].f] + l[v[x][i].s]);
mx[x] = max(mx[x] , mx[v[x][i].f] + l[v[x][i].s]);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
ll P;
for(int i=1; i<=n; i++){
cin >> P >> l[i];
v[P].pb(mp(i , i));
v[i].pb(mp(P , i));
}
ll cur,cur2,pre;
for(int s=1; s<=n; s++){
if(fix[s])continue;
len = 0;
ans = 0;
k = 0;
szg = 0;
szdis = 0;
findc(s , 0);
szc = 0;
szd = 0;
int ind = 0;
for(int i=0; i<szg; i++){
if(g[i] == g[szg - 1]){
ind = i + 1;
break;
}
}
for(int i=ind; i<szg; i++)
c[g[i]] = 1;
for(int i=ind; i<szg; i++){
findmx(g[i] , 0);
len += dis[i - 1];
}
cur = mx[g[ind]];
cur2 = mx[g[ind]];
pre = 0;
for(int i=ind + 1; i<szg; i++){
pre += dis[i - 1];
ans = max(ans , cur + mx[g[i]] + pre);
ans = max(ans , cur2 + len + mx[g[i]] - pre);
cur = max(cur , mx[g[i]] - pre);
cur2 = max(cur2 , mx[g[i]] + pre);
}
pas += ans;
}
cout << pas << '\n';
return 0;
}
Compilation message
islands.cpp: In function 'void findc(int, int)':
islands.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0; i<v[x].size(); i++)
| ~^~~~~~~~~~~~
islands.cpp: In function 'void findmx(int, int)':
islands.cpp:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0; i<v[x].size(); i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
3 |
Correct |
16 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23916 KB |
Output is correct |
5 |
Correct |
16 ms |
23916 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
15 ms |
23916 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
15 ms |
23916 KB |
Output is correct |
10 |
Correct |
15 ms |
23916 KB |
Output is correct |
11 |
Correct |
18 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24064 KB |
Output is correct |
2 |
Correct |
20 ms |
24172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
24684 KB |
Output is correct |
2 |
Correct |
36 ms |
26680 KB |
Output is correct |
3 |
Correct |
30 ms |
24940 KB |
Output is correct |
4 |
Correct |
22 ms |
24428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
27548 KB |
Output is correct |
2 |
Correct |
51 ms |
29564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
35108 KB |
Output is correct |
2 |
Correct |
99 ms |
37268 KB |
Output is correct |
3 |
Correct |
132 ms |
44984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
43788 KB |
Output is correct |
2 |
Correct |
222 ms |
60596 KB |
Output is correct |
3 |
Correct |
254 ms |
61164 KB |
Output is correct |
4 |
Correct |
273 ms |
77660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
67912 KB |
Output is correct |
2 |
Correct |
692 ms |
98144 KB |
Output is correct |
3 |
Correct |
280 ms |
64632 KB |
Output is correct |
4 |
Correct |
373 ms |
91244 KB |
Output is correct |
5 |
Correct |
385 ms |
91940 KB |
Output is correct |
6 |
Correct |
1100 ms |
72164 KB |
Output is correct |
7 |
Correct |
396 ms |
107628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
421 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |