Submission #500727

# Submission time Handle Problem Language Result Execution time Memory
500727 2021-12-31T23:20:44 Z Shreyan_Paliwal Carnival (CEOI14_carnival) C++17
100 / 100
16 ms 484 KB
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int n;

struct DSU {
  int         n;
  vector<int> link, size;

  DSU() : n(0), link(vector<int>(0)), size(vector<int>(0)) {}
  DSU(int n, vector<vector<int>> groups = {}) : n(n) {
    link = vector<int>(n), size = vector<int>(n);
    for (int i = 0; i < n; i++) {
      link[i] = i;
    }

    for (int i = 0; i < n; i++) size[i] = 1;

    for (const vector<int>& i : groups) {
      for (int j = 1; j < i.size(); j++) {
        unite(i[j], i[j - 1]);
      }
    }
  }

  int find(int x) {
    if (x == link[x]) return x;
    return link[x] = find(link[x]);
  }

  bool same(int a, int b) { return find(a) == find(b); }

  void unite(int a, int b) {
    int a_group = find(a), b_group = find(b);
    if (size[a_group] < size[b_group]) swap(a_group, b_group);

    size[a_group] += size[b_group];
    link[b_group] = a_group;
    link[b]       = a_group;
  }
};

struct VEC {
  vector<int> v = vector<int>(0);
  int         n = 0;

  VEC(int n) : v(vector<int>(n)), n(n) {}
  VEC(int n, int k) : v(vector<int>(n, k)), n(n) {}
  VEC(vector<int> v) : v(v), n(v.size()) {}

  bool operator<(const VEC& oth) const {
    for (int i = 0; i < min(v.size(), oth.v.size()); i++) {
      if (v[i] == oth.v[i]) continue;
      return v[i] < oth.v[i];
    }
    return v.size() < oth.v.size();
  }

  void push_back(int x) {
    v.push_back(x);
    n++;
  }
};

map<VEC, int> m;

// CREATE STRUCT FOR QUICK VECTOR COMPARISON IN MAP

int get_number(vector<int> v_) {
  VEC v = VEC(v_);
  if (m.find(v) == m.end()) {
    cout << v.n << " ";
    for (auto i : v.v) {
      cout << i + 1 << " ";
    }
    // cout << endl;
    fflush(stdout);
    int x;
    cin >> x;
    m[v] = x;
  }

  return m[v];
}

int get_number(VEC v) {
  if (m.find(v) == m.end()) {
    cout << v.n << " ";
    for (auto i : v.v) {
      cout << i + 1 << " ";
    }
    // cout << endl;
    fflush(stdout);
    int x;
    cin >> x;
    m[v] = x;
  }

  return m[v];
}

int main() {
  cin >> n;

  DSU dsu = DSU(n);

  VEC main_group = VEC(0);
  int counter    = 0;

  for (int i = 0; i < n; i++) {
    // cout << "----------" << endl;
    // MAKE GROUP
    int previous = counter;
    main_group.push_back(i);
    counter = get_number(main_group);
    if (counter == previous + 1) continue;
    int low = 0, high = i - 1;
    while (low != high) {
      int mid   = (low + high) / 2;
      VEC group = VEC(0);
      for (int j = low; j <= mid; j++) {
        group.push_back(j);
      }
      int prev = get_number(group);
      group.push_back(i);
      if (get_number(group) == prev + 1) {
        low = mid + 1;
      } else {
        high = mid;
      }
    }
    // cout << "UNITE " << low << " " << i << endl;
    dsu.unite(low, i);
  }

  map<int, int> mm;
  for (int i = 0, counter = 1; i < n; i++) {
    if (mm.find(dsu.find(i)) == mm.end()) {
      mm[dsu.find(i)] = counter++;
    }
  }

  cout << 0 << " ";
  for (int i = 0; i < n; i++) {
    cout << mm[dsu.find(i)] << " ";
  }

  return 0;
}

Compilation message

carnival.cpp: In constructor 'DSU::DSU(int, std::vector<std::vector<int> >)':
carnival.cpp:24:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |       for (int j = 1; j < i.size(); j++) {
      |                       ~~^~~~~~~~~~
carnival.cpp: In member function 'bool VEC::operator<(const VEC&) const':
carnival.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   56 |     for (int i = 0; i < min(v.size(), oth.v.size()); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 384 KB Output is correct
2 Correct 16 ms 348 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 3 ms 328 KB Output is correct
5 Correct 13 ms 392 KB Output is correct
6 Correct 13 ms 436 KB Output is correct
7 Correct 10 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 424 KB Output is correct
2 Correct 13 ms 480 KB Output is correct
3 Correct 8 ms 312 KB Output is correct
4 Correct 4 ms 364 KB Output is correct
5 Correct 7 ms 432 KB Output is correct
6 Correct 14 ms 404 KB Output is correct
7 Correct 13 ms 388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 404 KB Output is correct
2 Correct 7 ms 420 KB Output is correct
3 Correct 13 ms 416 KB Output is correct
4 Correct 4 ms 328 KB Output is correct
5 Correct 13 ms 360 KB Output is correct
6 Correct 11 ms 384 KB Output is correct
7 Correct 12 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 372 KB Output is correct
2 Correct 13 ms 484 KB Output is correct
3 Correct 8 ms 336 KB Output is correct
4 Correct 4 ms 372 KB Output is correct
5 Correct 11 ms 328 KB Output is correct
6 Correct 8 ms 328 KB Output is correct
7 Correct 14 ms 372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 456 KB Output is correct
2 Correct 16 ms 404 KB Output is correct
3 Correct 9 ms 360 KB Output is correct
4 Correct 10 ms 380 KB Output is correct
5 Correct 8 ms 328 KB Output is correct
6 Correct 4 ms 296 KB Output is correct
7 Correct 5 ms 368 KB Output is correct