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 "chameleon.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e2+10, MOD = 1e9+7;
namespace {
} // namespace
vector<int> adj[2 * N];
vector<int> vis;
void dfs_col(int c, int b) {
vis[c] = b;
for (int nxt : adj[c]) if (vis[nxt] == -1)
dfs_col(nxt, b ^ 1);
}
vector<int> build_cols(vector<pair<int, int>> edges, int l, int r) {
vis.assign(r + 1, -1);
for (int i = l; i <= r; i++)
adj[i].clear();
for (auto [a, b] : edges)
adj[a].push_back(b), adj[b].push_back(a);
for (int i = l; i <= r; i++) if (vis[i] == -1)
dfs_col(i, 0);
return vis;
}
vector<pair<int, int>> rec(int l, int r) {
if (l >= r) return {};
auto edges = rec(l + 1, r);
vector<int> cnt(r + 1);
for (auto [a, b] : edges)
cnt[a]++, cnt[b]++;
auto cols = build_cols(edges, l + 1, r);
vector<int> one, two;
for (int i = l + 1; i <= r; i++) {
if (cnt[i] == 3) continue;
if (cols[i])
one.push_back(i);
else
two.push_back(i);
}
for (int rep = 0; rep < 2; rep++) if (cnt[l] < 3) {
auto check = [&](int L, int R) {
vector<int> v{l};
for (int j = L; j <= R; j++)
v.push_back(one[j]);
if (Query(v) != sz(v))
return true;
else
return false;
};
int L = 0;
while (L < sz(one) && cnt[l] < 3) {
if (!check(L, sz(one)-1))
break;
int lo = -1, hi = sz(one)-1, mid = (lo + hi) / 2;
while (lo < mid && mid < hi) {
if (check(L, mid)) hi = mid;
else lo = mid;
mid = (lo + hi) / 2;
}
edges.emplace_back(l, one[hi]);
cnt[l]++, cnt[one[hi]]++;
L = hi + 1;
}
swap(one, two);
}
return edges;
}
void Solve(int n) {
auto edges = rec(1, 2*n);
vector<vector<int>> adj(2 * n + 1);
vector<int> out(2 * n + 1);
for (auto [a, b] : edges) {
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= 2 * n; i++) {
if (sz(adj[i]) == 1) {
continue;
}
assert(sz(adj[i]) == 3);
random_shuffle(adj[i].begin(), adj[i].end());
out[i] = adj[i][2];
for (int j = 0; j <= 1; j++) if (out[adj[i][j]] != i) {
vector<int> v{i};
for (int k = 0; k < 3; k++) if (k != j)
v.push_back(adj[i][k]);
if (Query(v) == 1) {
out[i] = adj[i][j];
break;
}
}
}
for (int a = 1; a <= 2 * n; a++) {
for (int b : adj[a]) {
if (out[a] == b) continue;
if (out[b] == a) continue;
if (a > b) continue;
Answer(a, b);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |