pub struct TwoSat { /* private fields */ }
Expand description
A 2-SAT Solver.
For variables $x_0, x_1, \ldots, x_{N - 1}$ and clauses with from
\[ (x_i = f) \lor (x_j = g) \]
it decides whether there is a truth assignment that satisfies all clauses.
§Example
#![allow(clippy::many_single_char_names)]
use ac_library::TwoSat;
use proconio::{input, marker::Bytes, source::once::OnceSource};
input! {
from OnceSource::from(
"3\n\
3\n\
a b\n\
!b c\n\
!a !a\n",
),
n: usize,
pqs: [(Bytes, Bytes)],
}
let mut twosat = TwoSat::new(n);
for (p, q) in pqs {
fn parse(s: &[u8]) -> (usize, bool) {
match *s {
[c] => ((c - b'a').into(), true),
[b'!', c] => ((c - b'a').into(), false),
_ => unreachable!(),
}
}
let ((i, f), (j, g)) = (parse(&p), parse(&q));
twosat.add_clause(i, f, j, g);
}
assert!(twosat.satisfiable());
assert_eq!(twosat.answer(), [false, true, true]);
Implementations§
Source§impl TwoSat
impl TwoSat
Sourcepub fn satisfiable(&mut self) -> bool
pub fn satisfiable(&mut self) -> bool
Returns whether there is a truth assignment that satisfies all clauses.
§Complexity
- $O(n + m)$ where $m$ is the number of added clauses
Sourcepub fn answer(&self) -> &[bool]
pub fn answer(&self) -> &[bool]
Returns a truth assignment that satisfies all clauses of the last call of satisfiable
.
§Constraints
satisfiable
is called after adding all clauses and it has returnedtrue
.
§Complexity
- $O(n)$
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TwoSat
impl RefUnwindSafe for TwoSat
impl Send for TwoSat
impl Sync for TwoSat
impl Unpin for TwoSat
impl UnwindSafe for TwoSat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more