POJ3678KatuPuzzle (2-SAT) poj. orgproblem? Id3678: A directed graph with N vertices and M edges. Each vertex can have 0 or 1. now each edge is marked by an operator (or, and, xor) and one (0 or 1), indicating that the result of a and B operations is (0 or 1 ). are there any feasible solutions for this directed graph? Analysis:
POJ 3678 Katu Puzzle (2-SAT) http://poj.org/problem? Id = 3678 question: a directed graph with N vertices and M edges. Each vertex can have 0 or 1. now each edge is marked by an operator (or, and, xor) and one (0 or 1), indicating that the result of a and B operations is (0 or 1 ). are there any feasible solutions for this directed graph? Analysis:
POJ 3678 Katu Puzzle (2-SAT)
Http://poj.org/problem? Id = 3678
Question:
A directed graph with N vertices and M edges. Each vertex can have 0 or 1 values. now each edge is marked by an operator (or, and, xor) and a value (0 or 1, it indicates that the result of the operator calculation for a and B is a value (0 or 1 ). are there any feasible solutions for this directed graph?
Analysis:
Since each vertex can only take 0 or 1 values, we convert this problem to a 2-SAT problem. each vertex in the source image corresponds to each vertex in 2-SAT. there are the following conversion methods for each operation:
A and B = 0 to a = 0 or B = 0
A and B = 1 to a = 1 and B = 1 to add the edge 2 * a-> 2 * a + 1 2 * B-> 2 * B + 1 (As long as a is 0 or B is 0, conflicts will inevitably occur.)
A or B = 0 to 2 * a + 1-> 2 * a 2 * B + 1-> 2 * B (As long as a is 1 or B is 1, conflicts will inevitably occur.)
A or B = 1 to a = 1 or B = 1
A xorb = 0 is converted to a = 1 and B = 1 or a = 0 and B = 0 is connected to the following edge:
2 * a-> 2 * B 2 * B-> 2 * a 2 * a + 1-> 2 * B + 1 2 * B + 1-> 2 * a + 1.
A xor B = 1 is converted to a = 1 and B = 0 or a = 0 and B = 1 is connected to the following edge:
2 * a + 1-> 2 * B 2 * B-> 2 * a + 1 2 * a-> 2 * B + 1 2 * B + 1-> 2 *
AC code:
# Include
# Include
# Include
Using namespace std; const int maxn = 1000 + 10; struct TwoSAT {int n; vector
G [maxn * 2]; int S [maxn * 2], c; bool mark [maxn * 2]; bool dfs (int x) {if (mark [x ^ 1]) return false; if (mark [x]) return true; mark [x] = true; S [c ++] = x; for (int I = 0; I
N = n; for (int I = 0; I
0) mark [S [-- c] = false; if (! Dfs (I + 1) return false ;}} return true ;}} TS; int main () {int n, m; scanf ("% d ", & n, & m); TS. init (n); int a, B, c; char op [10]; for (int I = 0; I