#include "chameleon.h"
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> edges; // edges(a, b) if Query({a, b}) = 1
void FindEdges(vector<int> candidates, int n) { // find edges from one of vertices in candidates to i
int lo = 0, hi = (int) candidates.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
vector<int> q(begin(candidates), begin(candidates) + mid + 1);
q.emplace_back(n);
if (Query(q) < q.size()) {
hi = mid;
} else {
lo = mid + 1;
}
}
edges.emplace_back(candidates[hi], n); // set(candidates[1...hi] + n) has an edge while set(candidates[1...hi-1] + n) doesn't, means there is an edge between candidates[hi] and n
vector<int> new_candidates(begin(candidates) + hi + 1, end(candidates));
vector<int> q(new_candidates);
q.emplace_back(n);
if (Query(q) < q.size()) { // there is an edge between new_candidates and n
return FindEdges(new_candidates, n);
}
}
void Solve(int N) {
vector<vector<int>> ind(4); // independent sets
for (int i = 1; i <= 2 * N; i++) {
vector<bool> is_independent(4, false); // is ind[j] independent after adding i?
for (int j = 0; j < 4; j++) {
vector<int> q(ind[j]);
q.emplace_back(i);
if (Query(q) < q.size()) { // there is an edge between ind[j] and i
FindEdges(ind[j], i);
} else {
is_independent[j] = true; // adding i to ind[j] still maintain its independency
}
}
for (int j = 0; j < 4; j++) {
if (is_independent[j]) { // adding i to ind[j] still maintain its independency, so we can put i in ind[j]
ind[j].emplace_back(i);
break;
}
}
}
vector<vector<int>> adj(2 * N + 1);
vector<bool> determined(2 * N + 1, false); // determined[i] means i has already been answered
for (auto &e : edges) {
int a = e.first, b = e.second;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
auto IsSpecial = [&](int a, int b) { // determine if a and b has the same color
vector<int> all_except_a_and_b;
for (int i = 1; i <= 2 * N; i++) {
if (i == a || i == b) continue;
all_except_a_and_b.emplace_back(i);
}
return Query(all_except_a_and_b) < N; // If query returns N, then a and b is not the same color
};
for (int i = 1; i <= 2 * N; i++) {
if (determined[i]) continue;
for (auto &j : adj[i]) {
if (determined[j]) continue;
if (IsSpecial(i, j)) {
determined[i] = true;
determined[j] = true;
Answer(i, j);
break;
}
}
}
}
Compilation message
chameleon.cpp: In function 'void FindEdges(std::vector<int>, int)':
chameleon.cpp:14:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (Query(q) < q.size()) {
~~~~~~~~~^~~~~~~~~~
chameleon.cpp:27:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (Query(q) < q.size()) { // there is an edge between new_candidates and n
~~~~~~~~~^~~~~~~~~~
chameleon.cpp: In function 'void Solve(int)':
chameleon.cpp:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (Query(q) < q.size()) { // there is an edge between ind[j] and i
~~~~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
39 ms |
384 KB |
Output is correct |
4 |
Correct |
39 ms |
504 KB |
Output is correct |
5 |
Correct |
39 ms |
384 KB |
Output is correct |
6 |
Correct |
38 ms |
384 KB |
Output is correct |
7 |
Correct |
39 ms |
384 KB |
Output is correct |
8 |
Correct |
38 ms |
384 KB |
Output is correct |
9 |
Correct |
40 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
360 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
360 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
64 ms |
428 KB |
Output is correct |
4 |
Correct |
63 ms |
384 KB |
Output is correct |
5 |
Correct |
65 ms |
384 KB |
Output is correct |
6 |
Correct |
61 ms |
384 KB |
Output is correct |
7 |
Correct |
67 ms |
384 KB |
Output is correct |
8 |
Correct |
61 ms |
384 KB |
Output is correct |
9 |
Correct |
61 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
39 ms |
384 KB |
Output is correct |
4 |
Correct |
39 ms |
504 KB |
Output is correct |
5 |
Correct |
39 ms |
384 KB |
Output is correct |
6 |
Correct |
38 ms |
384 KB |
Output is correct |
7 |
Correct |
39 ms |
384 KB |
Output is correct |
8 |
Correct |
38 ms |
384 KB |
Output is correct |
9 |
Correct |
40 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
360 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
64 ms |
428 KB |
Output is correct |
31 |
Correct |
63 ms |
384 KB |
Output is correct |
32 |
Correct |
65 ms |
384 KB |
Output is correct |
33 |
Correct |
61 ms |
384 KB |
Output is correct |
34 |
Correct |
67 ms |
384 KB |
Output is correct |
35 |
Correct |
61 ms |
384 KB |
Output is correct |
36 |
Correct |
61 ms |
384 KB |
Output is correct |
37 |
Correct |
57 ms |
384 KB |
Output is correct |
38 |
Correct |
39 ms |
384 KB |
Output is correct |
39 |
Correct |
66 ms |
384 KB |
Output is correct |
40 |
Correct |
64 ms |
384 KB |
Output is correct |
41 |
Correct |
57 ms |
384 KB |
Output is correct |
42 |
Correct |
40 ms |
384 KB |
Output is correct |
43 |
Correct |
61 ms |
384 KB |
Output is correct |
44 |
Correct |
58 ms |
424 KB |
Output is correct |
45 |
Correct |
58 ms |
384 KB |
Output is correct |