Submission #46031530
Source Code Expand
#include <bits/stdc++.h> using i64 = long long; struct TwoSat { int n; std::vector<std::vector<int>> e; std::vector<bool> ans; TwoSat(int n) : n(n), e(2 * n), ans(n) {} void addClause(int u, bool f, int v, bool g) { e[2 * u + !f].push_back(2 * v + g); e[2 * v + !g].push_back(2 * u + f); } bool satisfiable() { std::vector<int> id(2 * n, -1), dfn(2 * n, -1), low(2 * n, -1); std::vector<int> stk; int now = 0, cnt = 0; std::function<void(int)> tarjan = [&](int u) { stk.push_back(u); dfn[u] = low[u] = now++; for (auto v : e[u]) { if (dfn[v] == -1) { tarjan(v); low[u] = std::min(low[u], low[v]); } else if (id[v] == -1) { low[u] = std::min(low[u], dfn[v]); } } if (dfn[u] == low[u]) { int v; do { v = stk.back(); stk.pop_back(); id[v] = cnt; } while (v != u); ++cnt; } }; for (int i = 0; i < 2 * n; ++i) if (dfn[i] == -1) tarjan(i); for (int i = 0; i < n; ++i) { if (id[2 * i] == id[2 * i + 1]) return false; ans[i] = id[2 * i] > id[2 * i + 1]; } return true; } std::vector<bool> answer() { return ans; } }; std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); void solve() { int N; std::cin >> N; std::vector<std::vector<int>> adj(N); for (int i = 0; i < 3 * N / 2; i++) { int A, B; std::cin >> A >> B; A--, B--; adj[A].push_back(B); adj[B].push_back(A); } std::vector<int> ans(N); while (true) { for (int i = 0; i < N; i++) { ans[i] = rng() % 2; } TwoSat ts(N); for (int i = 0; i < N; i++) { ts.addClause(adj[i][0], ans[i], adj[i][1], ans[i]); ts.addClause(adj[i][1], ans[i], adj[i][2], ans[i]); ts.addClause(adj[i][2], ans[i], adj[i][0], ans[i]); } if (!ts.satisfiable()) { break; } } for (int i = 0; i < N; i++) { std::cout << "BW"[ans[i]]; } std::cout << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; std::cin >> T; while (T--) { solve(); } return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Not Dyed by Majority (Cubic Graph) |
User | jiangly |
Language | C++ 20 (gcc 12.2) |
Score | 800 |
Code Size | 2665 Byte |
Status | AC |
Exec Time | 54 ms |
Memory | 16868 KiB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 800 / 800 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 00-sample-001.txt |
All | 00-sample-001.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
00-sample-001.txt | AC | 1 ms | 3528 KiB |
01-001.txt | AC | 25 ms | 3476 KiB |
01-002.txt | AC | 24 ms | 3480 KiB |
01-003.txt | AC | 23 ms | 3412 KiB |
01-004.txt | AC | 24 ms | 3424 KiB |
01-005.txt | AC | 20 ms | 3424 KiB |
01-006.txt | AC | 22 ms | 3572 KiB |
01-007.txt | AC | 23 ms | 3476 KiB |
01-008.txt | AC | 23 ms | 3480 KiB |
01-009.txt | AC | 22 ms | 3384 KiB |
01-010.txt | AC | 23 ms | 3652 KiB |
01-011.txt | AC | 25 ms | 4832 KiB |
01-012.txt | AC | 22 ms | 3484 KiB |
01-013.txt | AC | 23 ms | 3504 KiB |
01-014.txt | AC | 22 ms | 3756 KiB |
01-015.txt | AC | 22 ms | 3476 KiB |
01-016.txt | AC | 22 ms | 3492 KiB |
01-017.txt | AC | 22 ms | 3704 KiB |
01-018.txt | AC | 22 ms | 3484 KiB |
01-019.txt | AC | 22 ms | 3452 KiB |
01-020.txt | AC | 22 ms | 3780 KiB |
01-021.txt | AC | 54 ms | 16760 KiB |
01-022.txt | AC | 46 ms | 16748 KiB |
01-023.txt | AC | 47 ms | 16640 KiB |
01-024.txt | AC | 46 ms | 16868 KiB |
01-025.txt | AC | 49 ms | 16736 KiB |
01-026.txt | AC | 50 ms | 16768 KiB |
01-027.txt | AC | 24 ms | 4776 KiB |
01-028.txt | AC | 24 ms | 4844 KiB |
01-029.txt | AC | 24 ms | 4848 KiB |
01-030.txt | AC | 24 ms | 4848 KiB |