Submission #500725

# Submission time Handle Problem Language Result Execution time Memory
500725 2021-12-31T23:10:27 Z Shreyan_Paliwal Carnival (CEOI14_carnival) C++17
0 / 100
16 ms 556 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);
  }

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

  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 Incorrect 11 ms 424 KB Integer 19 violates the range [1, 11]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 436 KB Integer 6 violates the range [1, 5]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 436 KB Output is correct
2 Incorrect 14 ms 404 KB Integer 11 violates the range [1, 8]
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 456 KB Integer 5 violates the range [1, 4]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 556 KB Output is correct
2 Incorrect 14 ms 400 KB Integer 20 violates the range [1, 17]
3 Halted 0 ms 0 KB -