Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 4;
- // Function to get cofactor of A[p][q] in temp[][]. n is current dimension of A[][]
- void getCofactor(int A[N][N], int temp[N][N], int p, int q, int n)
- {
- int i = 0, j = 0;
- // Looping for each element of the matrix
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- // Copying into temporary matrix only those element which are not in given row and column
- if (row != p && col != q)
- {
- temp[i][j++] = A[row][col];
- // Row is filled, so increase row index and reset col index
- if (j == n - 1)
- {
- j = 0;
- i++;
- }
- }
- }
- }
- }
- /* Recursive function for finding determinant of matrix. n is current dimension of A[][]. */
- int determinant(int A[N][N], int n)
- {
- int D = 0; // Initialize result
- // Base case : if matrix contains single element
- if (n == 1)
- return A[0][0];
- int temp[N][N]; // To store cofactors
- int sign = 1; // To store sign multiplier
- // Iterate for each element of first row
- for (int f = 0; f < n; f++)
- {
- // Getting Cofactor of A[0][f]
- getCofactor(A, temp, 0, f, n);
- D += sign * A[0][f] * determinant(temp, n - 1);
- // terms are to be added with alternate sign
- sign = -sign;
- }
- return D;
- }
- int main()
- {
- int n , m; cin>>n>>m;
- int A[N][N];
- memset(A , 0 , sizeof A);
- for(int i = 0; i<m; i++)
- {
- int v , u; cin>>v>>u;
- u--; v--;
- A[v][u]= A[u][v] = -1;
- A[v][v]++;
- A[u][u]++;
- }
- int copy[N][N];
- for(int i =1; i< n; i++)
- {
- for(int j = 1; j<n; j++)
- {
- copy[i-1][j-1] = A[i][j];
- }
- }
- cout<<determinant(copy,n-1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement