@@ -34,3 +34,75 @@ impl TwoSAT {
34
34
& self . answer
35
35
}
36
36
}
37
+
38
+ #[ cfg( test) ]
39
+ mod tests {
40
+ use super :: * ;
41
+ #[ test]
42
+ fn solve_alpc_h_sample1 ( ) {
43
+ // https://atcoder.jp/contests/practice2/tasks/practice2_h
44
+
45
+ let ( n, d) = ( 3 , 2 ) ;
46
+ let x = [ 1 , 2 , 0i32 ] ;
47
+ let y = [ 4 , 5 , 6 ] ;
48
+
49
+ let mut t = TwoSAT :: new ( n) ;
50
+
51
+ for i in 0 ..n {
52
+ for j in i + 1 ..n {
53
+ if ( x[ i] - x[ j] ) . abs ( ) < d {
54
+ t. add_clause ( i, false , j, false ) ;
55
+ }
56
+ if ( x[ i] - y[ j] ) . abs ( ) < d {
57
+ t. add_clause ( i, false , j, true ) ;
58
+ }
59
+ if ( y[ i] - x[ j] ) . abs ( ) < d {
60
+ t. add_clause ( i, true , j, false ) ;
61
+ }
62
+ if ( y[ i] - y[ j] ) . abs ( ) < d {
63
+ t. add_clause ( i, true , j, true ) ;
64
+ }
65
+ }
66
+ }
67
+ assert ! ( t. satisfiable( ) ) ;
68
+ let answer = t. answer ( ) ;
69
+ let mut res = vec ! [ ] ;
70
+ for ( i, & v) in answer. iter ( ) . enumerate ( ) {
71
+ if v {
72
+ res. push ( x[ i] )
73
+ } else {
74
+ res. push ( y[ i] ) ;
75
+ }
76
+ }
77
+ assert_eq ! ( res, [ 4 , 2 , 0 ] ) ;
78
+ }
79
+
80
+ #[ test]
81
+ fn solve_alpc_h_sample2 ( ) {
82
+ // https://atcoder.jp/contests/practice2/tasks/practice2_h
83
+
84
+ let ( n, d) = ( 3 , 3 ) ;
85
+ let x = [ 1 , 2 , 0i32 ] ;
86
+ let y = [ 4 , 5 , 6 ] ;
87
+
88
+ let mut t = TwoSAT :: new ( n) ;
89
+
90
+ for i in 0 ..n {
91
+ for j in i + 1 ..n {
92
+ if ( x[ i] - x[ j] ) . abs ( ) < d {
93
+ t. add_clause ( i, false , j, false ) ;
94
+ }
95
+ if ( x[ i] - y[ j] ) . abs ( ) < d {
96
+ t. add_clause ( i, false , j, true ) ;
97
+ }
98
+ if ( y[ i] - x[ j] ) . abs ( ) < d {
99
+ t. add_clause ( i, true , j, false ) ;
100
+ }
101
+ if ( y[ i] - y[ j] ) . abs ( ) < d {
102
+ t. add_clause ( i, true , j, true ) ;
103
+ }
104
+ }
105
+ }
106
+ assert ! ( !t. satisfiable( ) ) ;
107
+ }
108
+ }
0 commit comments