# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
221642 |
2020-04-10T17:25:47 Z |
rama_pang |
ICC (CEOI16_icc) |
C++14 |
|
6 ms |
512 KB |
// Solution:
// - Maintain Disjoint Components of all Vertices
// - Every time a new road is added, we first find out how to separate
// current components into 2 sets such that the new edge connect them
// - Consider how to check if two sets are connectd with the new edge. If yes,
// we have found the split. If no, we recurse to set1 and set2. However
// we can optimize this, by noticing that when we query the first half of
// set1 to find if there is an edge in second half of set1 or not, as well
// as set2, we can merge these queries together: we query the first half
// of set1 and set2 with the second half of set1 and set2, since set1 and
// set2 is edge disjoint this doesn't affect answer. With this, we can
// find such a cut in O(log N).
// - Binary search for the affected vertex in each disjoint set, to get
// the new edge.
// - Upper bound is 3 N log N queries, this pass for 90 points.
// - We try to find 2 connected components A and B directly.
// - We can find A xor B by querying for every bit, we query the components with
// on bit and off bit. If it returns 1, then A xor B for that bit = 1.
// - Let X be an on bit in A xor B. We try to recover A and B. Assume X-th bit
// in A is off and X-th bit in B is on.
// - If the i-th bit of A xor B is 0, we query sets where set1 has 0 in the i-th
// bit and 0 in the X-th bit, and set2 has 0 in the i-th bit and 1 in the X-th
// bit. If it returns 1, that means A = B = 0 in the i-th bit. Otherwise, A = B = 1.
// - If the i-th bit of A xor B is 1, we query sets where set1 has 0 in the i-th
// bit and 0 in the X-th bit, and set2 has 1 in the i-th bit and 1 in the X-th
// bit. If it returns 1, that means A = 0 and B = 1 in the i-th bit. Otherwise, A = 1
// and B = 0.
// - We found A and B. Now find the endpoint in the same way as before.
// - This approach uses approximately 1600 queries, and passes for full points.
#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);
}
}
Compilation message
icc.cpp: In function 'std::pair<int, int> BinarySearch(std::vector<int>, std::vector<int>, bool)':
icc.cpp:69:8: warning: unused variable 'res' [-Wunused-variable]
bool res;
^~~
icc.cpp: In function 'std::pair<int, int> RecoverFromXor(int)':
icc.cpp:114:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < components.size(); k++) {
~~^~~~~~~~~~~~~~~~~~~
icc.cpp:128: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:154:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < components.size(); k++) {
~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
512 KB |
Wrong road! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
512 KB |
Wrong road! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
512 KB |
Wrong road! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
512 KB |
Wrong road! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
512 KB |
Wrong road! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
512 KB |
Wrong road! |
2 |
Halted |
0 ms |
0 KB |
- |