This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
int ct = 0;
for (auto v: G[u]){
if (vis[v.fi] == 2){
if (v.fi == p){
if (ct == 1){
//printf("%d %d repeated\n",u,v.fi);
incyc[u] = 1;
join = v.se;
}
else {
ct++;
}
continue;
}
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);
//printf("at %d\n",root);
if (P.size() == 2){
return max(ans,d[P[0].fi]+d[P[1].fi]+max(P[0].se,P[1].se));
}
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 (stderr)
islands.cpp: In function 'll solve(int)':
islands.cpp:69:15: warning: variable 'x' set but not used [-Wunused-but-set-variable]
for (auto x : P){
^
islands.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < P.size(); i++){
~~^~~~~~~~~~
islands.cpp:80: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:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
islands.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&j,&L);
~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |