Poj 1830 switch problem, poj1830
Switch Problems
Give the initial and final states of the n (0 <n <29) Switch (01 ), and the relationship between switches (a B indicates a-> B). There are several ways to get the final state. Otherwise, the literal value of the output character.
1. difference from poj 1222: Association is one-way, and each switch needs to be pre-processed for its own Association (start to process its own association in the input association relationship, WA has two hair ), the operation matrix (transformed matrix) is the initial state XOR final state;
2. After processing, determine whether the final result a [k] [var] with all the coefficients 0 is used to determine whether there is no solution. At the same time, if there are n free yuan, because each yuan only has two states, so there are only 2 ^ n solutions.
# Include <iostream> # include <cstdio> # include <cstring> # include <string. h> # include <algorithm> # include <map> # include <queue> # include <vector> # include <cmath> # include <stdlib. h> # include <time. h> using namespace std; # define rep0 (I, l, r) for (int I = (l); I <(r); I ++) # define rep1 (I, l, r) for (int I = (l); I <= (r); I ++) # define rep_0 (I, r, l) for (int I = (r); I> (l); I --) # define rep_1 (I, r, l) for (int I = (r ); i> = (l); I --) # def Ine MS0 (a) memset (a, 0, sizeof (a) # define MS1 (a) memset (a,-1, sizeof ()) int a [35] [35]; int equ, var; int x [35]; void debug () {int I, j; rep0 (I, 0, equ) {rep1 (j, 0, var) cout <a [I] [j] <""; cout <endl ;}} int Guass () {int I, j, k, free_var = 0, row, col; for (row = 0, col = 0; row <equ & col <var; row ++, col ++) {int mx = row; rep0 (j, row + 1, equ) if (abs (a [j] [col])> abs (a [mx] [col]) mx = j; if (a [mx] [col] = 0) {row --;// Rows remain unchanged; free_var ++; continue;} if (mx! = Row) rep1 (k, col, var) swap (a [row] [k], a [mx] [k]); rep0 (j, row + 1, equ) {if (a [j] [col]) {// The j lamp will also affect the I lamp. rep1 (k, col, var) a [j] [k] ^ = a [row] [k] ;}}// debug (); for (; row <equ; row ++) if (a [row] [var]! = 0) return-1; // no solution; if (free_var! = 0) return free_var; rep_1 (I, var-1, 0) {// The number of variables to be solved, not the number of equations; x [I] = a [I] [var]; rep0 (j, I + 1, equ) x [I] ^ = (a [I] [j] & x [j]); // The lamp number j affects the lamp number I, at the same time, the j lamp will also be on.} Return 0;} void init () {int I, j, k, n; MS0 (a); MS0 (x); scanf ("% d", & n ); equ = var = n; int x, l, r; rep0 (I, 0, n) scanf ("% d", & a [I] [n]); rep0 (I, 0, n) scanf ("% d", & x), a [I] [n] ^ = x; rep0 (I, 0, n) a [I] [I] = 1; // assign values unless there is no association; while (scanf ("% d", & l, & r) = 2 & l + r) {a [r-1] [IL-1] = 1 ;}} int main () {int T, kase = 1, I; cin> T; while (T --) {init (); int ret = Guass (); if (ret =-1) puts ("Oh, it's impossible ~!! "); Else printf (" % d \ n ", 1 <ret) ;}return 0 ;}View Code