Submission #448464

#TimeUsernameProblemLanguageResultExecution timeMemory
448464prvocisloIslands (IOI08_islands)C++17
80 / 100
888 ms131076 KiB
#include <iostream> #include <vector> #include <algorithm> typedef long long ll; using namespace std; const int maxn = 1e6 + 5; vector<bool> vis(maxn, false); vector<pair<int, ll> > p(maxn, { -1, 0 }); vector<vector<pair<int, int> > > g(maxn); void dfs1(int u, int& a, int& b, ll &l, vector<int>& v) { v.push_back(u); vis[u] = true; for (const pair<int, int>& i : g[u]) { if (!vis[i.first]) p[i.first] = { u, i.second }, dfs1(i.first, a, b, l, v); else if (i.first != p[u].first) a = i.first, b = u, l = i.second; } } pair<ll, int> dfs2(int u, int p, ll d) { pair<ll, int> ans(d, u); for (const pair<int, int>& i : g[u]) if (!vis[i.first] && i.first != p) ans = max(ans, dfs2(i.first, u, d + i.second)); return ans; } void del(int u, int v, int l) { for (int i = 0; i < g[u].size(); i++) if (g[u][i] == make_pair(v, l)) { g[u].erase(g[u].begin() + i); break; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0, u, l; i < n; i++) cin >> u >> l, g[--u].push_back({ i, l }), g[i].push_back({ u, l }); ll ans = 0; for (int i = 0; i < n; i++) if (!vis[i]) { int u, v; ll l; vector<int> c; dfs1(i, u, v, l, c); // u je ten nizsi, v je ten vyssi del(u, v, l), del(v, u, l); for (int j : c) vis[j] = false; int v1 = dfs2(i, -1, 0).second; ll maxi = dfs2(v1, -1, 0).first; vector<int> path; for (int j = u; j != p[v].first; j = p[j].first) path.push_back(j), vis[j] = true; int k = path.size(); vector<ll> best, pf(k, 0), sf(k, 0); for (int j : path)best.push_back(dfs2(j, -1, 0).first); ll len = 0; for (int j = 0; j < path.size(); j++) { if (j) len += p[path[j-1]].second; pf[j] = len + best[j]; if (j) pf[j] = max(pf[j], pf[j - 1]); } len = 0; for (int j = path.size() - 1; j >= 0; j--) { if (j != path.size()-1) len += p[path[j]].second; sf[j] = len + best[j]; if (j != path.size() - 1) sf[j] = max(sf[j], sf[j + 1]); } for (int j = 0; j+1 < path.size(); j++) maxi = max(maxi, l + pf[j] + sf[j + 1]); ans += maxi; for (int j : c) vis[j] = true; } cout << ans << "\n"; return 0; }

Compilation message (stderr)

islands.cpp: In function 'void del(int, int, int)':
islands.cpp:29: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]
   29 |     for (int i = 0; i < g[u].size(); i++) if (g[u][i] == make_pair(v, l))
      |                     ~~^~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:57:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for (int j = 0; j < path.size(); j++)
      |                         ~~^~~~~~~~~~~~~
islands.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             if (j != path.size()-1) len += p[path[j]].second;
      |                 ~~^~~~~~~~~~~~~~~~
islands.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |             if (j != path.size() - 1) sf[j] = max(sf[j], sf[j + 1]);
      |                 ~~^~~~~~~~~~~~~~~~~~
islands.cpp:69:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         for (int j = 0; j+1 < path.size(); j++)
      |                         ~~~~^~~~~~~~~~~~~
#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...