This topic is the initial and final state of N lamps, and some relationships are given. These relationships indicate that the switch may affect the switch of other lamps, this relationship can be regarded as an inverse relationship.
For this question, we assume a set of data:
3
0 1 0
1 1 0
1 2
2 3
1 3
3 2
0 0
For the above data, we use a matrix to represent the entire operation process and status.
0 1
S = 1 e = 1
0 0
We can clearly understand that the combination of various switches will be reflected in a certain equal bright condition. Therefore, we can obtain such a equations, where the diplus is equivalent to an exclusive or operation:
E (A) = XA * A11 ^ XB * A12 ^ XC * A13 ^ s ();
E (B) = XA * A21 ^ XB * A22 ^ XC * A23 ^ s (B );
E (c) = XA * A31 ^ XB * A32 ^ XC * A33 ^ s (C );
S (a) indicates the initial state, E (a) indicates the final state of lamp A, XA indicates whether the switch a is pressed, a13 indicates whether Switch 3 affects the light-off of Lamp 1. An exception or operation reflects the specific impact of the operation.
Moving the s term to the left of the equation, we abstract the coefficient matrix and construct the Augmented Matrix. Then, we perform Gaussian elimination, the Gaussian elimination element can be seen as a transformation of the variable element requirements in the case of a series of two logical relationships. However, all the operations used here are exclusive or operations. In the end, we determine the rank. If the rank of the matrix is not the same as that of the adjacent matrix, there will be no solution. If the rank is the same, it indicates that the number of switches in at least certain States must be the rank of the matrix when the desired light is on or off. The solution is to select any remaining switch, that is, 2 ^ X (each switch is switched off or on ).
The Code is as follows:
# Include <cstdlib> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; int N, S [50], E [50]; void swap (Int & A, Int & B) {int T = A; A = B; B = T;} struct matrix {int A [50] [50]; void rswap (int x, int y) {for (Int J = 1; j <= n + 1; ++ J) {swap (A [x] [J], A [y] [J]) ;}} void relax (int x, int y) {for (Int J = 1; j <= n + 1; ++ J) {A [y] [J] ^ = A [x] [J] ;}} void Init () {memset (A, 0, sizeof (A); For (INT I = 1; I <= N; ++ I) {A [I] [I] = 1 ;}} m; void Gauss () {int PTR, T, I, J; for (I = 1, j = 1; I <= N, j <= N; ++ I, ++ J) {for (PTR = I; PTR <= N; ++ PTR) {If (M. A [PTR] [J]) {// when column J of row I is not zero break;} If (PTR = n + 1) {-- I; continue ;} if (PTR! = I) {M. rswap (PTR, I) ;}for (int K = I + 1; k <= N; ++ K) {If (! M. A [k] [J]) {continue;} M. relax (I, K) ;}}for (int K = I; k <= N; ++ K) {If (M. A [k] [n + 1]! = 0) {puts ("oh, it's impossible ~!! "); Return ;}} printf (" % d \ n ", 1 <(n-I + 1) ;}int main () {int T,, b; scanf ("% d", & T); While (t --) {scanf ("% d", & N); M. init (); For (INT I = 1; I <= N; ++ I) {scanf ("% d", & S [I]);} for (INT I = 1; I <= N; ++ I) {scanf ("% d", & E [I]); M. A [I] [n + 1] = s [I] ^ e [I];} while (scanf ("% d", & A, & B ), A | B) {M. A [B] [a] = 1;} Gauss ();} return 0 ;}