Submission #1054743

#TimeUsernameProblemLanguageResultExecution timeMemory
1054743lovrotIslands (IOI08_islands)C++17
15 / 100
185 ms131072 KiB
#include <cstdio> #include <vector> #include <algorithm> #define debug(...) //fprintf(stderr, __VA_ARGS__) #define X first #define Y second #define PB push_back using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 1e6 + 10; int n, weg[N]; vector<pii> g[N]; int bio[N]; vector<pii> cyc; int dfs(int u, int p) { bio[u] = 1; for(pii e : g[u]) { int v = e.X, w = e.Y; if(w != p) { if(bio[v]) { cyc.PB({e.X, weg[e.Y]}); return v; } else { int res = dfs(v, w); if(res) { cyc.PB({e.X, weg[e.Y]}); return res != u ? res : 0; } } } } bio[u] = 0; return 0; } void mark(int u) { bio[u] = 2; for(pii e : g[u]) { if(bio[e.X] != 2) { mark(e.X); } } } ll dep[N]; ll depth(int u, int p) { for(pii e : g[u]) { int v = e.X, w = e.Y; if(w != p && !bio[v]) { dep[u] = max(dep[u], (ll) weg[w] + depth(v, w)); } } return dep[u]; } int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { int u, w; scanf("%d%d", &u, weg + i); g[i].PB({u, i}); g[u].PB({i, i}); } ll ans = 0; for(int i = 1; i <= n; ++i) { if(bio[i]) { continue; } cyc.clear(); dfs(i, 0); for(pii e : cyc) { depth(e.X, 0); debug("%d(%d, %lld)\n", e.X, e.Y, dep[e.X]); } ll res = dep[cyc.front().X], xam = dep[cyc.front().X], prf = cyc.front().Y; for(int i = 1; i < cyc.size(); ++i) { res = max(dep[cyc[i].X] + xam + prf, res); xam = max(dep[cyc[i].X] - prf, xam); prf += cyc[i].Y; } xam = dep[cyc.front().X]; prf = 0; for(int i = (int) cyc.size() - 1; i > 0; --i) { prf += cyc[i].Y; res = max(dep[cyc[i].X] + xam + prf, res); xam = max(dep[cyc[i].X] - prf, xam); } ans += res; mark(i); debug("+ %lld\n", res); } printf("%lld\n", ans); return 0; }

Compilation message (stderr)

islands.cpp: In function 'int main()':
islands.cpp:68:10: warning: unused variable 'w' [-Wunused-variable]
   68 |   int u, w;
      |          ^
islands.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for(int i = 1; i < cyc.size(); ++i) {
      |                  ~~^~~~~~~~~~~~
islands.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |   scanf("%d%d", &u, weg + i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...