Submission #1022669

#TimeUsernameProblemLanguageResultExecution timeMemory
1022669PurpleCrayonChameleon's Love (JOI20_chameleon)C++17
100 / 100
31 ms656 KiB
#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 {}; if (r - l + 1 < 2) { vector<pair<int, int>> edges; for (int i = l; i <= r; i++) for (int j = l; j < i; j++) if (Query({i, j}) != 2) edges.emplace_back(i, j); return edges; } int m = (l + r) / 2; /* if (r - l > 10 && rand() % 10 == 0) { m--; } */ if (r - l > 10) m = l + (r - l) / 3; auto edges_one = rec(l, m), edges_two = rec(m + 1, r); vector<pair<int, int>> edges; for (auto e : edges_one) edges.push_back(e); for (auto e : edges_two) edges.push_back(e); vector<int> cnt(r + 1); for (auto [a, b] : edges) cnt[a]++, cnt[b]++; auto cols = build_cols(edges_two, m + 1, r); vector<int> one, two; for (int i = m + 1; i <= r; i++) { if (cnt[i] == 3) continue; if (cols[i]) one.push_back(i); else two.push_back(i); } for (int i = l; i <= m; i++) if (cnt[i] < 3) { for (int rep = 0; rep < 2; rep++) if (cnt[i] < 3) { auto check = [&](int L, int R) { vector<int> v{i}; 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[i] < 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(i, one[hi]); cnt[i]++, 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...