Submission #557648

#TimeUsernameProblemLanguageResultExecution timeMemory
557648iancuTowns (IOI15_towns)C++14
100 / 100
25 ms816 KiB
#include "towns.h" #include <vector> #include <map> using namespace std; class DSU { private: vector<int> par; map<int, int> poz; public: DSU(const vector<int>& v) { for (int i = 0; i < v.size(); ++i) poz[v[i]] = i; for (int i = 0; i < v.size(); ++i) par.push_back(i); } int getRoot(int x) { if (x == par[x]) return x; return par[x] = getRoot(par[x]); } bool query(int a, int b) { a = poz[a], b = poz[b]; int ra = getRoot(a); int rb = getRoot(b); return ra == rb; } void join(int a, int b) { a = poz[a], b = poz[b]; int ra = getRoot(a); int rb = getRoot(b); par[ra] = rb; } bool isRoot(int x) { x = poz[x]; return x == getRoot(x); } int getSize(int x) { x = getRoot(poz[x]); int sz = 0; for (int i = 0; i < par.size(); ++i) sz += (getRoot(i) == x); return sz; } }; int hubDistance(int N, int sub) { vector<vector<int>> dist(N, vector<int>(N, -1)); auto getDist = [&dist](int a, int b) { if (dist[a][b] == -1) dist[a][b] = dist[b][a] = getDistance(a, b); return dist[a][b]; }; int D1, D2, dist_max = 0; for (int i = 1; i < N; ++i) if (getDist(0, i) > dist_max) { dist_max = getDist(0, i); D1 = i; } dist_max = 0; for (int i = 0; i < N; ++i) if (i != D1 && getDist(D1, i) > dist_max) { dist_max = getDist(D1, i); D2 = i; } int R = getDist(D1, D2); for (int i = 0; i < N; ++i) if (i != D1 && i != 0) { int dist_i = (getDist(i, D1) - getDist(i, 0) + getDist(D1, 0)) / 2; R = min(R, max(dist_i, getDist(D1, D2) - dist_i)); } vector<int> leaves1, leaves2; int cnt1 = 0, cnt2 = 0; int st = 0, dr = 0; for (int i = 0; i < N; ++i) { int dist_i = (getDist(i, D1) - getDist(i, 0) + getDist(D1, 0)) / 2; if (R == max(dist_i, getDist(D1, D2) - dist_i)) { if (R == dist_i) { ++cnt1; leaves1.push_back(i); } else { ++cnt2; leaves2.push_back(i); } } else if (dist_i < getDist(D1, D2) - dist_i) { ++st; } else { ++dr; } } if (st > N / 2 || dr > N / 2) return -R; int R_init = R; vector<int> leaves; if (leaves1.size() > leaves2.size()) { leaves = leaves1; } else { leaves = leaves2; R = getDist(D1, D2) - R; } auto sameSubtree = [&dist, &D1, &D2, &getDist, &R](int a, int b) { int dist_lca = (getDist(a, D1) + getDist(b, D1) - getDist(a, b)) / 2; if (dist_lca <= R) return false; return true; }; if (leaves.empty()) return R_init; DSU dsu(leaves); vector<int> cand = leaves; int fin = leaves.back(); while (!cand.empty()) { vector<int> new_cand; for (int i = 0; i < cand.size(); i += 2) { if (i < cand.size() - 1) { if (sameSubtree(cand[i], cand[i + 1])) { dsu.join(cand[i], cand[i + 1]); new_cand.push_back(cand[i]); } } else { fin = cand[i]; } } cand = new_cand; } int ap = 0; for (auto i: leaves) if (dsu.isRoot(i) && sameSubtree(fin, i)) ap += dsu.getSize(i); if (ap > N / 2) return -R_init; return R_init; }

Compilation message (stderr)

towns.cpp: In constructor 'DSU::DSU(const std::vector<int>&)':
towns.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (int i = 0; i < v.size(); ++i)
      |                     ~~^~~~~~~~~~
towns.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for (int i = 0; i < v.size(); ++i)
      |                     ~~^~~~~~~~~~
towns.cpp: In member function 'int DSU::getSize(int)':
towns.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i = 0; i < par.size(); ++i)
      |                     ~~^~~~~~~~~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |     for (int i = 0; i < cand.size(); i += 2) {
      |                     ~~^~~~~~~~~~~~~
towns.cpp:116:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |       if (i < cand.size() - 1) {
      |           ~~^~~~~~~~~~~~~~~~~
towns.cpp:48:28: warning: unused parameter 'sub' [-Wunused-parameter]
   48 | int hubDistance(int N, int sub) {
      |                        ~~~~^~~
towns.cpp:51:15: warning: 'D1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   51 |     if (dist[a][b] == -1)
      |               ^
towns.cpp:52:44: warning: 'D2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   52 |       dist[a][b] = dist[b][a] = getDistance(a, b);
      |                                 ~~~~~~~~~~~^~~~~~
#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...