This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include "library.h"
const int N = 1000;
vector<int> adj[N];
vector<int> m, pos, res;
vector<pair<int, bool>> v;
bool isEnd[N];
int n_;
void dfs(int v, int p = -1) {
res.push_back(v + 1);
for (int i : adj[v]) {
if (i != p)
dfs(i, v);
}
}
int get() {
int ans = 0;
for (int i = 0; i < n_; i++) {
if (adj[i].size() == 1 && !isEnd[i])
ans = i;
}
return ans;
}
int find(int num, int l, int r) {
if (l == r) {
m[num] = 1;
m[pos[l]] = 1;
if (Query(m) == 1)
return pos[l];
else
return -1;
}
int mid = (l + r) / 2;
for (int i = l; i <= mid; i++)
m[pos[i]] = 1;
m[num] = 1;
int a1 = Query(m);
m[num] = 0;
int a2 = Query(m);
for (int i = l; i <= mid; i++)
m[pos[i]] = 0;
if (a2 < a1)
return find(num, mid + 1, r);
else
return find(num, l, mid);
}
void Solve(int n) {
n_ = n;
m.resize(n, 0);
for (int i = 0; i < n; i++)
v.emplace_back(i, true);
for (int i = 0; i < n - 1;) {
int num = get();
pos.clear();
for (auto j : v) {
if (j.second && j.first != num)
pos.push_back(j.first);
}
fill(m.begin(), m.end(), 0);
int ans = find(num, 0, (int)pos.size() - 1);
if (ans != -1)
adj[num].push_back(ans), adj[ans].push_back(num), i++, v[num].second = false, v[ans].second = false;
else
isEnd[num] = true;
}
int end1 = -1;
for (int i = 0; i < n; i++) {
if (isEnd[i] || adj[i].size() == 1)
end1 = i;
}
dfs(end1);
for (int i : res)
assert(i <= n);
Answer(res);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |