# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
221641 |
2020-04-10T17:25:32 Z |
rama_pang |
ICC (CEOI16_icc) |
C++14 |
|
0 ms |
0 KB |
#include "icc.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<vector<int>> components;
int Query(vector<int> A, vector<int> B) {
if (A.empty() || B.empty()) return 0;
return query(A.size(), B.size(), A.data(), B.data());
}
int QueryComponent(vector<int> A, vector<int> B) {
vector<int> P, Q;
for (auto i : A) {
for (auto j : components[i]) {
P.emplace_back(j);
}
}
for (auto i : B) {
for (auto j : components[i]) {
Q.emplace_back(j);
}
}
return Query(P, Q);
}
void Answer(int A, int B) {
return setRoad(A, B);
}
pair<int, int> BinarySearch(vector<int> A, vector<int> B, bool onComponent) {
if (A.size() < B.size()) swap(A, B);
if (A.size() == 1 && B.size() == 1) return {A[0], B[0]};
int mid = A.size() / 2;
vector<int> L(begin(A), begin(A) + mid), R(begin(A) + mid, end(A));
bool res;
if (onComponent) {
if (R.empty() || QueryComponent(L, B)) {
return BinarySearch(L, B, onComponent);
} else {
return BinarySearch(R, B, onComponent);
}
} else {
if (R.empty() || Query(L, B)) {
return BinarySearch(L, B, onComponent);
} else {
return BinarySearch(R, B, onComponent);
}
}
}
void MergeComponent(int C1, int C2) {
vector<int> Merged;
for (auto i : components[C1]) Merged.emplace_back(i);
for (auto i : components[C2]) Merged.emplace_back(i);
components[C1].clear();
components[C2].clear();
sort(begin(components), end(components), [&](const vector<int> &a, const vector<int> &b) {
return a.size() > b.size();
});
while (!components.empty() && components.back().empty()) components.pop_back();
components.emplace_back(Merged);
}
pair<int, int> RecoverFromXor(int Xor) {
int X = 0;
int resA = 0, resB = 0;
for (int i = 0; i < 7; i++) {
if (Xor & (1 << i)) {
X = i;
resB |= 1 << i;
break;
}
}
for (int i = 0; i < 7; i++) {
if (i == X) continue;
if (Xor & (1 << i)) { // i-th bit in Xor is ON, so we query check if A = 0 and B = 1 or otherwise
vector<int> A, B;
for (int k = 0; k < components.size(); k++) {
if ((k & (1 << i)) && (k & (1 << X))) { // i-th and X-th bit is ON
B.emplace_back(k);
} else if (!(k & (1 << i)) && !(k & (1 << X))) {
A.emplace_back(k);
}
}
if (QueryComponent(A, B)) { // A = 0 and B = 1
resB |= 1 << i;
} else {
resA |= 1 << i;
}
} else { // Check if A = B = 0 or A = B = 1
vector<int> A, B;
for (int k = 0; k < components.size(); k++) {
if ((k & (1 << i)) && (k & (1 << X))) { // i-th and X-th bit is ON
B.emplace_back(k);
} else if (!(k & (1 << i)) && !(k & (1 << X))) {
A.emplace_back(k);
}
}
if (QueryComponent(A, B)) { // A = 1 and B = 1
resA |= 1 << i;
resB |= 1 << i;
}
}
}
return {resA, resB};
}
void run(int N) {
for (int i = 1; i <= N; i++) {
components.emplace_back(vector<int>{i});
}
for (int i = 1; i < N; i++) {
int Xor = 0;
for (int j = 0; j < 7; j++) {
vector<int> A, B;
for (int k = 0; k < components.size(); k++) {
if (k & (1 << j)) {
A.emplace_back(k);
} else {
B.emplace_back(k);
}
}
if (QueryComponent(A, B)) {
Xor |= 1 << j;
}
}
// Binary Search to find edge's endpoint
auto TwoComp = RecoverFromXor(Xor);
auto Edge = BinarySearch(components[TwoComp.first], components[TwoComp.second], false);
Answer(Edge.first, Edge.second);
MergeComponent(TwoComp.first, TwoComp.second);
}
}
int main() {
int N;
cin >> N;
run(N);
}
Compilation message
icc.cpp: In function 'std::pair<int, int> BinarySearch(std::vector<int>, std::vector<int>, bool)':
icc.cpp:39:8: warning: unused variable 'res' [-Wunused-variable]
bool res;
^~~
icc.cpp: In function 'std::pair<int, int> RecoverFromXor(int)':
icc.cpp:84:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < components.size(); k++) {
~~^~~~~~~~~~~~~~~~~~~
icc.cpp:98:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < components.size(); k++) {
~~^~~~~~~~~~~~~~~~~~~
icc.cpp: In function 'void run(int)':
icc.cpp:124:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < components.size(); k++) {
~~^~~~~~~~~~~~~~~~~~~
/tmp/ccnC1Y7d.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/cc7zwa0B.o:icc.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status