#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);
}
int Split(int L, int R, vector<int> &A, vector<int> &B, int depth, bool Check) {
if (depth == 0) {
int mid = (L + R) / 2;
for (int i = L; i <= mid; i++) A.emplace_back(i);
for (int i = mid + 1; i <= R; i++) B.emplace_back(i);
return max(mid - L + 1, R - mid);
}
int mid = (L + R) / 2;
if (Check) {
return max(Split(L, mid, A, B, depth - 1, Check), Split(mid + 1, R, A, B, depth - 1, Check));
} else {
vector<int> tA, tB;
Split(L, mid, tA, tB, depth - 1, true);
if (QueryComponent(tA, tB)) {
return Split(L, mid, A, B, depth - 1, Check);
} else {
return Split(mid + 1, R, A, B, depth - 1, Check);
}
}
}
pair<int, int> BinarySearch(vector<int> A, vector<int> B, bool onComponent) {
pair<int, int> res = {-1, -1};
{
int lo = 0, hi = A.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
vector<int> halfA;
for (int i = 0; i <= mid; i++) {
halfA.emplace_back(A[i]);
}
int res = (onComponent ? QueryComponent(halfA, B) : Query(halfA, B));
if (res) {
hi = mid;
} else {
lo = mid + 1;
}
}
res.first = A[lo];
}
{
int lo = 0, hi = B.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
vector<int> halfB;
for (int i = 0; i <= mid; i++) {
halfB.emplace_back(B[i]);
}
int res = (onComponent ? QueryComponent(halfB, A) : Query(halfB, A));
if (res) {
hi = mid;
} else {
lo = mid + 1;
}
}
res.second = B[lo];
}
return res;
}
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.back().empty()) components.pop_back();
components.emplace_back(Merged);
}
void run(int N) {
for (int i = 1; i <= N; i++) {
components.emplace_back(vector<int>{i});
}
for (int i = 1; i < N; i++) {
shuffle(begin(components), end(components), rnd);
vector<int> A, B; // find two components that are connected by new edge
for (int depth = 0; ; depth++) {
int sz = Split(0, components.size() - 1, A, B, depth, true);
if (sz == 1) break;
int res = QueryComponent(A, B);
A.clear(), B.clear();
if (res) {
Split(0, components.size() - 1, A, B, depth, false);
break;
}
}
// Binary Search to find edge's endpoint
auto TwoComp = BinarySearch(A, B, true);
auto Edge = BinarySearch(components[TwoComp.first], components[TwoComp.second], false);
Answer(Edge.first, Edge.second);
MergeComponent(TwoComp.first, TwoComp.second);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
512 KB |
Ok! 110 queries used. |
2 |
Correct |
11 ms |
512 KB |
Ok! 109 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
512 KB |
Ok! 613 queries used. |
2 |
Correct |
48 ms |
512 KB |
Ok! 564 queries used. |
3 |
Correct |
50 ms |
512 KB |
Ok! 600 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
632 KB |
Ok! 1536 queries used. |
2 |
Correct |
167 ms |
512 KB |
Ok! 1471 queries used. |
3 |
Correct |
163 ms |
512 KB |
Ok! 1629 queries used. |
4 |
Correct |
158 ms |
512 KB |
Ok! 1518 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
512 KB |
Ok! 1644 queries used. |
2 |
Correct |
161 ms |
640 KB |
Ok! 1602 queries used. |
3 |
Correct |
173 ms |
512 KB |
Ok! 1606 queries used. |
4 |
Correct |
157 ms |
640 KB |
Ok! 1543 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
512 KB |
Ok! 1562 queries used. |
2 |
Correct |
165 ms |
512 KB |
Ok! 1600 queries used. |
3 |
Correct |
183 ms |
632 KB |
Ok! 1593 queries used. |
4 |
Correct |
179 ms |
512 KB |
Ok! 1608 queries used. |
5 |
Correct |
158 ms |
572 KB |
Ok! 1529 queries used. |
6 |
Correct |
149 ms |
512 KB |
Ok! 1476 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
192 ms |
584 KB |
Too many queries! 1636 out of 1625 |
2 |
Halted |
0 ms |
0 KB |
- |