Submission #262161

#TimeUsernameProblemLanguageResultExecution timeMemory
262161dantoh000Islands (IOI08_islands)C++14
80 / 100
945 ms131076 KiB
#include <bits/stdc++.h> #define fi first #define se second ///Input format is not always nice, first nodes may not be in the keychain ring 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; int cyclehead; void dfs(int u, int p){ vis[u] = 2; int join =0; int ct = 0; for (auto v: G[u]){ if (vis[v.fi] == 2){ if (v.fi == p){ if (ct == 1){ cyclehead = p; //printf("%d %d repeated\n",u,v.fi); incyc[u] = 1; join = v.se; } else { ct++; } continue; } cyclehead = v.fi; incyc[u] = 1; join = v.se; continue; } if (vis[v.fi]) continue; dfs(v.fi,u); if (incyc[v.fi] && v.fi != cyclehead) { incyc[u] = 1; join = v.se; continue; } } if (incyc[u]){ //printf("at %d : %d\n",u,join); P.push_back({u,join}); } vis[u] = 1; } void dfs2(int u, int p){ ll d2 = 0; for (auto v: G[u]){ if (v.fi == p || incyc[v.fi]) continue; dfs2(v.fi,u); 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]); } ll solve(int root){ ans = 0; P.clear(); dfs(root,-1); //printf("at %d\n",root); reverse(P.begin(),P.end()); for (auto x : P){ dfs2(x.fi,-1); //printf("%d: %d %d\n",x.fi,d[x.fi],x.se); } if (P.size() == 2){ return max(ans,d[P[0].fi]+d[P[1].fi]+max(P[0].se,P[1].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:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i = 1; i < P.size(); i++){
      |                     ~~^~~~~~~~~~
islands.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for (int i = 1; i < P.size(); i++){
      |                     ~~^~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
islands.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  109 |         scanf("%d%d",&j,&L);
      |         ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...