#include "park.h"
#include <bits/stdc++.h>
using namespace std;
void Detect(int T, int N) {
vector<int> graph;
vector<int> candidate;
vector<pair<int, int>> edges;
graph.emplace_back(0);
for (int i = 1; i < N; i++) {
candidate.emplace_back(i);
}
const auto CreateVector = [&](int l, int r, vector<int> a) {
vector<int> res;
for (int i = l; i <= r; i++) {
res.emplace_back(a[i]);
}
return res;
};
const auto IsConnected = [&](int node, vector<int> g) -> bool {
vector<int> query(N, 0);
for (auto i : g) {
query[i] = 1;
}
query[node] = 1;
return Ask(min(g[0], node), max(g[0], node), query.data());
};
const auto IsConnectedTrough = [&](int node, vector<int> g, vector<int> add) -> bool {
vector<int> query(N, 0);
for (auto i : g) {
query[i] = 1;
}
for (auto i : add) {
query[i] = 1;
}
query[node] = 1;
return Ask(min(g[0], node), max(g[0], node), query.data());
};
function<void(int, vector<int>)> Connect = [&](int node, vector<int> g) -> void {
if (g.empty()) return;
vector<vector<int>> adj(N);
vector<int> active(N);
for (auto i : g) {
active[i] = 1;
}
for (auto e : edges) {
if (active[e.first] && active[e.second]) {
adj[e.first].emplace_back(e.second);
adj[e.second].emplace_back(e.first);
}
}
vector<int> vis;
vector<int> invis(N, 0);
vis.emplace_back(g[0]);
invis[g[0]] = 1;
for (int ptr = 0; ptr < (int) vis.size(); ptr++) {
int u = vis[ptr];
for (auto v : adj[u]) {
if (!invis[v]) {
invis[v] = 1;
vis.emplace_back(v);
}
}
}
if (vis.size() != g.size()) {
vector<int> p1, p2;
for (auto i : g) {
if (invis[i]) {
p1.emplace_back(i);
} else {
p2.emplace_back(i);
}
}
Connect(node, p1);
Connect(node, p2);
return;
}
if (!IsConnected(node, CreateVector(0, int(vis.size()) - 1, vis))) {
return;
}
int lo = 0, hi = int(vis.size()) - 1;
while (lo < hi) {
int md = (lo + hi) / 2;
if (IsConnected(node, CreateVector(0, md, vis))) {
hi = md;
} else {
lo = md + 1;
}
}
edges.emplace_back(node, vis[lo]);
g.erase(find(begin(g), end(g), vis[lo]));
return Connect(node, g);
};
while (!candidate.empty()) {
int node = candidate.back();
vector<int> path;
function<void(int, int)> FindPath = [&](int l, int r) {
vector<int> c;
if (l == 0) {
c = graph;
} else {
c = {l};
}
if (IsConnected(r, c)) {
path.emplace_back(r);
} else {
// Find a node on path from l to r
int lo = 0, hi = int(candidate.size()) - 1;
while (lo < hi) {
int md = (lo + hi) / 2;
if (IsConnectedTrough(r, c, CreateVector(0, md, candidate))) {
hi = md;
} else {
lo = md + 1;
}
}
FindPath(l, candidate[lo]);
FindPath(candidate[lo], r);
}
};
FindPath(0, node);
for (auto u : path) {
Connect(u, graph);
graph.emplace_back(u);
candidate.erase(find(begin(candidate), end(candidate), u));
}
}
for (auto e : edges) {
Answer(min(e.first, e.second), max(e.first, e.second));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
27 ms |
384 KB |
Output is correct |
3 |
Correct |
25 ms |
512 KB |
Output is correct |
4 |
Correct |
49 ms |
512 KB |
Output is correct |
5 |
Correct |
116 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
731 ms |
888 KB |
Output is correct |
2 |
Correct |
705 ms |
1144 KB |
Output is correct |
3 |
Correct |
642 ms |
888 KB |
Output is correct |
4 |
Correct |
736 ms |
760 KB |
Output is correct |
5 |
Correct |
727 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
714 ms |
912 KB |
Output is correct |
2 |
Correct |
769 ms |
928 KB |
Output is correct |
3 |
Correct |
781 ms |
972 KB |
Output is correct |
4 |
Correct |
750 ms |
1068 KB |
Output is correct |
5 |
Correct |
790 ms |
1084 KB |
Output is correct |
6 |
Correct |
786 ms |
932 KB |
Output is correct |
7 |
Correct |
764 ms |
1044 KB |
Output is correct |
8 |
Correct |
780 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
732 KB |
Output is correct |
2 |
Correct |
763 ms |
932 KB |
Output is correct |
3 |
Correct |
758 ms |
948 KB |
Output is correct |
4 |
Correct |
732 ms |
840 KB |
Output is correct |
5 |
Correct |
714 ms |
1032 KB |
Output is correct |
6 |
Correct |
730 ms |
1104 KB |
Output is correct |
7 |
Correct |
669 ms |
992 KB |
Output is correct |
8 |
Correct |
718 ms |
1104 KB |
Output is correct |
9 |
Correct |
708 ms |
948 KB |
Output is correct |
10 |
Correct |
810 ms |
1124 KB |
Output is correct |
11 |
Correct |
759 ms |
1164 KB |
Output is correct |
12 |
Correct |
742 ms |
1044 KB |
Output is correct |
13 |
Correct |
656 ms |
1192 KB |
Output is correct |
14 |
Correct |
775 ms |
1012 KB |
Output is correct |
15 |
Correct |
661 ms |
980 KB |
Output is correct |
16 |
Correct |
771 ms |
1076 KB |
Output is correct |
17 |
Correct |
727 ms |
776 KB |
Output is correct |
18 |
Correct |
782 ms |
1148 KB |
Output is correct |
19 |
Correct |
687 ms |
1032 KB |
Output is correct |
20 |
Correct |
765 ms |
924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
842 ms |
1252 KB |
Output is correct |
2 |
Correct |
825 ms |
1152 KB |
Output is correct |
3 |
Correct |
794 ms |
1128 KB |
Output is correct |
4 |
Correct |
754 ms |
956 KB |
Output is correct |
5 |
Correct |
842 ms |
1136 KB |
Output is correct |
6 |
Correct |
737 ms |
1108 KB |
Output is correct |
7 |
Correct |
848 ms |
1240 KB |
Output is correct |
8 |
Correct |
733 ms |
956 KB |
Output is correct |
9 |
Correct |
794 ms |
976 KB |
Output is correct |
10 |
Correct |
779 ms |
1068 KB |
Output is correct |
11 |
Correct |
739 ms |
1004 KB |
Output is correct |
12 |
Correct |
806 ms |
1160 KB |
Output is correct |
13 |
Correct |
717 ms |
1140 KB |
Output is correct |
14 |
Correct |
834 ms |
1212 KB |
Output is correct |
15 |
Correct |
788 ms |
1072 KB |
Output is correct |
16 |
Correct |
767 ms |
1196 KB |
Output is correct |
17 |
Correct |
735 ms |
888 KB |
Output is correct |
18 |
Correct |
780 ms |
908 KB |
Output is correct |