Switching problems
Test instructions: To the initial and final states of the N (0 < n < 29) switch (01), and the correlation between the switches (the association is a one-way input A-B represents a->b), there are several ways to get the final state. Otherwise, the output character literal.
1. The difference from the POJ 1222: The association is one-way, you need to preprocess each switch to its own association (start in the input association to deal with its own association, WA two), the operation of the matrix (transformation of the matrix) for the initial state of the XOR final state;
2. The final result of the judgment coefficient of 0 after processing A[k][var] whether the zero solution is determined. At the same time, if there are n free variable elements, because there are only two states of each variable, there are only 2^n schemes.
#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 namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))inta[ *][ *];intEquvar;intx[ *];voidDebug () {inti,j; Rep0 (i,0, Equ) {Rep1 (J,0,var) cout<<a[i][j]<<" "; cout<<Endl; }}intGuass () {intI,j,k,free_var =0, Row,col; for(row =0, col =0; row < equ && Col <var; row++,col++){ intMX =Row; Rep0 (J,row+1, equ)if(ABS (A[j][col]) > abs (A[mx][col])) MX =J; if(A[mx][col] = =0) {row--;//The line does not change;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 first lamp will also have an effect on the I lampREP1 (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)returnFree_var; Rep_1 (i,var-1,0){//The number of variables to be solved is not the number of equations;X[i] = a[i][var]; Rep0 (J,i+1, equ) x[i]^= (A[i][j] && x[j]);//The J Lantern will affect the first lamp and the first J lamp is also lit. } return 0;}voidinit () {intI,j,k,n; MS0 (a); MS0 (x); scanf ("%d",&N); Equ=var=N; intX,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;//there is no correlation to assign value; while(SCANF ("%d%d", &l,&r) = =2&& L +R) {A[r-1][l-1] =1; }}intMain () {intT,kase =1, I; CIN>>u; while(t--) {init (); intRET =Guass (); if(ret = =-1) puts ("oh,it ' s impossible~!!"); Elseprintf"%d\n",1<<ret); } return 0;}
View Code
POJ 1830 switch problem