POJ 1830 Gaussian Elimination

switch problem
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9883   Accepted: 3971

Description

There are N identical switches, each switch is associated with some switches, whenever you turn on or off a switch, other switches associated with this switch will also change accordingly, that is, these are associated The state of the switch becomes OFF if it was originally ON, and ON if it is OFF. Your goal is to make the last N switches reach a specific state after a number of switching operations. For any one switch, only one switch operation can be performed at most. Your task is to count how many ways a given state can be reached. (regardless of the sequence of switch operations)

Input

There is a number K in the first line of input, indicating that there are K groups of test data below. 
The format of each set of test data is as follows: 
the first line is a number N (0 < N < 29), 
and the second line is N numbers of 0 or 1, indicating N switch states at the beginning. 
The third row of N numbers of 0 or 1 indicates the state of the N switches after the operation is completed. 
Next, there are two numbers IJ in each line, indicating that if the I-th switch is operated, the state of the J-th switch will also change. Each set of data ends with 0 0. 

Output

If there is a feasible way, output the total number, otherwise output "Oh,it's impossible~!!" Do not include quotation marks

Sample Input

2
3
0 0 0
1 1 1
1 2
1 3
2 1
2 3
3 1
3 2
0 0
3
0 0 0
1 0 1
1 2
2 1
0 0

Sample Output

4
Oh,it's impossible~!!

 

Ideas:
Gaussian elimination: a*X=b
If the x-th switch is connected to the y-th switch, then a[y][x]=1 in the coefficient matrix, a[i][i]=1 in the initialization, the initial situation and the last situation or the following is our b matrix .
 
Code:
 1 //#include"bits/stdc++.h"
 2 #include<sstream>
 3 #include<iomanip>
 4 #include"cstdio"
 5 #include"map"
 6 #include"set"
 7 #include"cmath"
 8 #include"queue"
 9 #include"vector"
10 #include"string"
11 #include"cstring"
12 #include"time.h"
13 #include"iostream"
14 #include"stdlib.h"
15 #include"algorithm"
16 #define db double
17 #define ll long long
18 #define vec vector<ll>
19 #define mt  vector<vec>
20 #define ci(x) scanf("%d",&x)
21 #define cd(x) scanf("%lf",&x)
22 #define cl(x) scanf("%lld",&x)
23 #define pi(x) printf("%d\n",x)
24 #define pd(x) printf("%f\n",x)
25 #define pl(x) printf("%lld\n",x)
26 //#define rep(i, x, y) for(int i=x;i<=y;i++)
27 #define rep(i, n) for(int i=0;i<n;i++)
28 const int N   = 1e2+ 5;
29 //const int mod = 1e9 + 7;
30 //const int MOD = mod - 1;
31 const int inf = 0x3f3f3f3f;
32 const db  PI  = acos(-1.0);
33 const db  eps = 1e-10 ;
 34  using  namespace std;
 35  int equ, var ; // equ equations, var variables, the number of rows of the augmented matrix is ​​equ, the number of columns is var+1, respectively 0 to var 
36  int a[N][ N]; // Augmented matrix 
37  int x[N]; // Store free variables 
38  int f_x[N];
 39  int free_x; // Number of free variables 
40  void swap( int &x, int & y) {
 41      int t;
 42      t=x,x=y,y= t;
 43  }
 44 int Gauss()
45 {
46     int ma_r,col,k;
47     free_x=0;
48     for(k=0,col=0;k<equ&&col<var;k++,col++){
49         ma_r = k;
50         for (int i = k + 1; i < equ; i++) if (abs(a[i][col] > abs(a[ma_r][col]))) ma_r = i;//取系数最大的一行
51         if (!a[ma_r][col]) {
52             k--;
53             f_x[free_x++] =col;
 54              continue ;
 55          }
 56          if (ma_r != k)
 57              for ( int j = col; j < var + 1 ; j++) swap(a[k][j], a[ma_r][j]); // Swap 
58 with current row  
59          for ( int i = k + 1 ; i < equ; i++ )
 60              if (a[i][col] != 0 )
 61                  for ( int j = col; j < var + 1 ; j++) a[i][j] ^= a[k][j]; // eliminate the variables in the col column of other rows
62      }
 63      for ( int i=k;i<equ;i++) if (a[i][col]!= 0 ) return - 1 ; // No solution if not eliminated 
64  
65      if (k< var ) return  var -k; // Number of free variables
 66      // The only solution, back to 
67      for ( int i= var - 1 ;i>= 0 ;i-- ){
 68          x[i]=a[i][ var ];
 69          for ( int j=i+1;j<var;j++) x[i]^=(a[i][j]&&x[j]);//自下而上
70     }
71     return 0;
72 }
73 int n;
74 int main()
75 {
76     int t;
77     ci(t);
78     for(int I=1;I<=t;I++){
79         memset(a,0,sizeof(a));
80         ci(n);
81         int x, y;
82         for (int i = 0; i < n; i++) ci(a[i][n]);
83         for (int i = 0; i < n; i++) ci(x), a[i][n] ^= x;
84         for (int i = 0; i < n; i++) a[i][i] = 1;
85         while (scanf("%d%d", &x, &y), x + y) a[y-1][x-1] = 1;
86         equ = n, var = n;
87         int ans = Gauss();
88         if(ans == -1) puts("Oh,it's impossible~!!");
89         else
90             pl(1ll << ans);
91     }
92     return 0;
93 }

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324656962&siteId=291194637