#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
int n;
vector<ii> G[1000005];
vector<ii> P;
int incyc[1000005];
int vis[1000005];
ll d[1000005];
ll Sl[1000005];
ll ANS;
ll ans;
void dfs(int u, int p){
vis[u] = 2;
int join =0;
ll d2 = 0;
for (auto v: G[u]){
if (v.fi == p) continue;
if (vis[v.fi] == 2){
incyc[u] = 1;
join = v.se;
continue;
}
if (vis[v.fi]) continue;
dfs(v.fi,u);
if (incyc[v.fi]) {
incyc[u] = 1;
join = v.se;
continue;
}
ll nd = d[v.fi]+v.se;
if (d[u] <= nd){
d2 = d[u];
d[u] = nd;
}
else d2 = max(d2,nd);
}
ans = max(ans,d2+d[u]);
if (incyc[u]){
//printf("at %d : %d\n",u,join);
P.push_back({u,join});
}
vis[u] = 1;
}
ll solve(int root){
ans = 0;
P.clear();
dfs(root,-1);
if (P.size() == 0){
return max(G[root][0].se, G[root][1].se);
}
//printf("at %d\n",root);
reverse(P.begin(),P.end());
for (auto x : P){
//printf("%d: %d %d\n",x.fi,d[x.fi],x.se);
}
ll S = P[0].se;
Sl[P[0].fi] = 0;
for (int i = 1; i < P.size(); i++){
Sl[P[i].fi] = Sl[P[i-1].fi]+P[i-1].se;
S += P[i].se;
}
ll MX = d[P[0].fi];
ll MX2 = d[P[0].fi];
for (int i = 1; i < P.size(); i++){
int x = P[i].fi;
//printf("at %lld %lld\n",d[x],Sl[x]);
//printf("MX: %lld + %lld\n",MX,d[x] + Sl[x]);
//printf("MX2: %lld + %lld\n",MX2,S + d[x] - Sl[x]);
ans = max(ans, d[x] + Sl[x] + MX);
ans = max(ans, S + d[x] - Sl[x] + MX2);
MX = max(MX, d[x] - Sl[x]);
MX2 = max(MX2, d[x] + Sl[x]);
}
//printf("%lld max\n",ans);
return ans;
}
int main(){
scanf("%d",&n);
for (int i = 1; i <= n; i++){
int j,L;
scanf("%d%d",&j,&L);
G[i].push_back({j,L});
G[j].push_back({i,L});
}
for (int i = 1; i <= n; i++){
if (vis[i] == 0){
ANS += solve(i);
}
}
printf("%lld",ANS);
}
Compilation message
islands.cpp: In function 'll solve(int)':
islands.cpp:58:15: warning: variable 'x' set but not used [-Wunused-but-set-variable]
for (auto x : P){
^
islands.cpp:64:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < P.size(); i++){
~~^~~~~~~~~~
islands.cpp:70:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < P.size(); i++){
~~^~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
islands.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&j,&L);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
18 ms |
23808 KB |
Output isn't correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Incorrect |
16 ms |
23808 KB |
Output isn't correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Incorrect |
16 ms |
23808 KB |
Output isn't correct |
7 |
Incorrect |
16 ms |
23808 KB |
Output isn't correct |
8 |
Incorrect |
18 ms |
23808 KB |
Output isn't correct |
9 |
Incorrect |
17 ms |
23808 KB |
Output isn't correct |
10 |
Incorrect |
18 ms |
23808 KB |
Output isn't correct |
11 |
Correct |
18 ms |
23784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
23936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
25208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
29684 KB |
Output is correct |
2 |
Correct |
63 ms |
32756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
40568 KB |
Output is correct |
2 |
Correct |
120 ms |
44528 KB |
Output is correct |
3 |
Incorrect |
157 ms |
57996 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
51832 KB |
Output is correct |
2 |
Correct |
247 ms |
78312 KB |
Output is correct |
3 |
Correct |
274 ms |
81768 KB |
Output is correct |
4 |
Correct |
360 ms |
104928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
445 ms |
69092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
499 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |