#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,p[N],l[N],fix[N],F[N],k,c[N];
long long len;
long long mx[N],ans,pas;
vector<pair<int,int> >v[N];
vector<int>g,all,dis;
void dfs(int x){
if(F[x])return;
all.pb(x);
F[x] = 1;
for(int i=0; i<v[x].size(); i++)
dfs(v[x][i].f);
}
void findc(int x,int par){
if(fix[x]){
g.pb(x);
dis.pb(l[par]);
k = 1;
return;
}
g.pb(x);
fix[x] = 1;
for(int i=0; i<v[x].size(); i++)
if(v[x][i].s != par){
dis.pb(l[v[x][i].s]);
findc(v[x][i].f , v[x][i].s);
if(k)return;
dis.pop_back();
}
g.pop_back();
}
void findmx(int x,int par){
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]);
}
}
}
main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<=n; i++){
cin >> p[i] >> l[i];
v[p[i]].pb(mp(i , i));
v[i].pb(mp(p[i] , i));
}
ll cur,cur2,pre;
for(int s=1; s<=n; s++){
if(F[s])continue;
len = 0;
ans = 0;
k = 0;
all.clear();
dfs(s);
g.clear();
dis.clear();
findc(s , 0);
vector<int>cyc,d;
for(int i=0; i<g.size(); i++){
if(g[i] == g[(int)g.size() - 1]){
for(int j=i+1; j<g.size(); j++){
cyc.pb(g[j]);
d.pb(dis[j - 1]);
c[g[j]] = 1;
}
break;
}
}
for(int i=0; i<cyc.size(); i++){
findmx(cyc[i] , 0);
len += d[i];
}
cur = mx[cyc[0]];
cur2 = mx[cyc[0]];
pre = 0;
for(int i=1; i<cyc.size(); i++){
pre += d[i];
ans = max(ans , cur + mx[cyc[i]] + pre);
ans = max(ans , cur2 + len + mx[cyc[i]] - pre);
cur = max(cur , mx[cyc[i]] - pre);
cur2 = max(cur2 , mx[cyc[i]] + pre);
}
pas += ans;
}
cout << pas << '\n';
}
Compilation message
islands.cpp: In function 'void dfs(int)':
islands.cpp:18: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]
18 | for(int i=0; i<v[x].size(); i++)
| ~^~~~~~~~~~~~
islands.cpp: In function 'void findc(int, int)':
islands.cpp:30: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]
30 | for(int i=0; i<v[x].size(); i++)
| ~^~~~~~~~~~~~
islands.cpp: In function 'void findmx(int, int)':
islands.cpp:40: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]
40 | for(int i=0; i<v[x].size(); i++){
| ~^~~~~~~~~~~~
islands.cpp: At global scope:
islands.cpp:48:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main(){
| ^
islands.cpp: In function 'int main()':
islands.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0; i<g.size(); i++){
| ~^~~~~~~~~
islands.cpp:71:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int j=i+1; j<g.size(); j++){
| ~^~~~~~~~~
islands.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i=0; i<cyc.size(); i++){
| ~^~~~~~~~~~~
islands.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i=1; i<cyc.size(); i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
16 ms |
23916 KB |
Output is correct |
3 |
Correct |
19 ms |
23916 KB |
Output is correct |
4 |
Correct |
18 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 |
16 ms |
23916 KB |
Output is correct |
8 |
Correct |
17 ms |
23916 KB |
Output is correct |
9 |
Correct |
16 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24044 KB |
Output is correct |
2 |
Correct |
18 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25088 KB |
Output is correct |
2 |
Correct |
37 ms |
27624 KB |
Output is correct |
3 |
Correct |
28 ms |
25452 KB |
Output is correct |
4 |
Correct |
23 ms |
24684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
29036 KB |
Output is correct |
2 |
Correct |
54 ms |
31764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
39264 KB |
Output is correct |
2 |
Correct |
114 ms |
43008 KB |
Output is correct |
3 |
Correct |
136 ms |
52960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
49780 KB |
Output is correct |
2 |
Correct |
230 ms |
75616 KB |
Output is correct |
3 |
Correct |
239 ms |
77580 KB |
Output is correct |
4 |
Correct |
298 ms |
111572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
375 ms |
85320 KB |
Output is correct |
2 |
Correct |
853 ms |
129364 KB |
Output is correct |
3 |
Correct |
321 ms |
86956 KB |
Output is correct |
4 |
Correct |
430 ms |
126260 KB |
Output is correct |
5 |
Correct |
424 ms |
127668 KB |
Output is correct |
6 |
Correct |
1326 ms |
101236 KB |
Output is correct |
7 |
Runtime error |
425 ms |
131072 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
441 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |