This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |