-
Notifications
You must be signed in to change notification settings - Fork 29
Implement twosat #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement twosat #43
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9591c79
Implement 'twosat'
manta1130 dbacf02
Fixed mistake
manta1130 3bba0ca
Add tests
manta1130 c0862e8
Suppress warnings
manta1130 2a69b8b
Changed the struct name in accoding with UpperCamelCase rule.
manta1130 c57c408
Changed the return value type(&Vec<bool> -> &[bool])
manta1130 cac3df7
Changed if expression into explicit type conversion (bool -> usize).
manta1130 4b0747b
Changed the test code
manta1130 209a624
Added #[derive(Default)] in some structs
manta1130 d6a667c
Revert "Added #[derive(Default)] in some structs"
manta1130 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,114 @@ | ||
use crate::internal_scc; | ||
|
||
pub struct TwoSat { | ||
n: usize, | ||
scc: internal_scc::SccGraph, | ||
answer: Vec<bool>, | ||
} | ||
impl TwoSat { | ||
pub fn new(n: usize) -> Self { | ||
TwoSat { | ||
n, | ||
answer: vec![false; n], | ||
scc: internal_scc::SccGraph::new(2 * n), | ||
} | ||
} | ||
pub fn add_clause(&mut self, i: usize, f: bool, j: usize, g: bool) { | ||
assert!(i < self.n && j < self.n); | ||
self.scc.add_edge(2 * i + !f as usize, 2 * j + g as usize); | ||
self.scc.add_edge(2 * j + !g as usize, 2 * i + f as usize); | ||
} | ||
pub fn satisfiable(&mut self) -> bool { | ||
let id = self.scc.scc_ids().1; | ||
for i in 0..self.n { | ||
if id[2 * i] == id[2 * i + 1] { | ||
return false; | ||
} | ||
self.answer[i] = id[2 * i] < id[2 * i + 1]; | ||
} | ||
true | ||
} | ||
pub fn answer(&self) -> &[bool] { | ||
&self.answer | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#![allow(clippy::many_single_char_names)] | ||
use super::*; | ||
#[test] | ||
fn solve_alpc_h_sample1() { | ||
// https://atcoder.jp/contests/practice2/tasks/practice2_h | ||
|
||
let (n, d) = (3, 2); | ||
let x = [1, 2, 0i32]; | ||
let y = [4, 5, 6]; | ||
|
||
let mut t = TwoSat::new(n); | ||
|
||
for i in 0..n { | ||
for j in i + 1..n { | ||
if (x[i] - x[j]).abs() < d { | ||
t.add_clause(i, false, j, false); | ||
} | ||
if (x[i] - y[j]).abs() < d { | ||
t.add_clause(i, false, j, true); | ||
} | ||
if (y[i] - x[j]).abs() < d { | ||
t.add_clause(i, true, j, false); | ||
} | ||
if (y[i] - y[j]).abs() < d { | ||
t.add_clause(i, true, j, true); | ||
} | ||
} | ||
} | ||
assert!(t.satisfiable()); | ||
let answer = t.answer(); | ||
let mut res = vec![]; | ||
for (i, &v) in answer.iter().enumerate() { | ||
if v { | ||
res.push(x[i]) | ||
} else { | ||
res.push(y[i]); | ||
} | ||
} | ||
|
||
//Check the min distance between flags | ||
res.sort(); | ||
let mut min_distance = i32::max_value(); | ||
for i in 1..res.len() { | ||
min_distance = std::cmp::min(min_distance, res[i] - res[i - 1]); | ||
} | ||
assert!(min_distance >= d); | ||
} | ||
|
||
#[test] | ||
fn solve_alpc_h_sample2() { | ||
// https://atcoder.jp/contests/practice2/tasks/practice2_h | ||
|
||
let (n, d) = (3, 3); | ||
let x = [1, 2, 0i32]; | ||
let y = [4, 5, 6]; | ||
|
||
let mut t = TwoSat::new(n); | ||
|
||
for i in 0..n { | ||
for j in i + 1..n { | ||
if (x[i] - x[j]).abs() < d { | ||
t.add_clause(i, false, j, false); | ||
} | ||
if (x[i] - y[j]).abs() < d { | ||
t.add_clause(i, false, j, true); | ||
} | ||
if (y[i] - x[j]).abs() < d { | ||
t.add_clause(i, true, j, false); | ||
} | ||
if (y[i] - y[j]).abs() < d { | ||
t.add_clause(i, true, j, true); | ||
} | ||
} | ||
} | ||
assert!(!t.satisfiable()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.