HUT-1558 Count Cycles
1558: Count Cycles
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 84 Solved: 21
[Submit][Status][Web Board]
Description
In information theory, a low-density parity-check (LDPC) code is a linear error correcting code, a method of transmitting a message over a noisy transmission channel, and is constructed using a sparse bipartite graph. LDPC codes are capacity-approaching codes, which means that practical constructions exist that allow the noise threshold to be set very close (or even arbitrarily close on the BEC) to the theoretical maximum (the Shannon limit) for a symmetric memory-less channel.
LDPC codes are defined by a sparse parity-check matrix. This parity-check matrix is often randomly generated and the elements in it are 0 or 1. If we want use LDPC codes, we should make the parity-check matrix have no cycles. When four vertices of the rectangle in the matrix are 1, we say that the matrix has one cycle. Now we want to know how many cycles are in the matrix.
For a given matrix, you are to count the number of cycles in the matrix.
Input
There are several test cases, each test case starts with a line containing two positive integers M and N. M and N is the size of the matrix (1<=M<=100, 1<=N<=100). Next follow a matrix which contains only number 0 and 1. The input will finish with the end of file.
Output
For each the case, your program will output the number of cycles in the given matrix on separate line.
Sample Input
Sample Output
#include <cstdio> #include <cstring> #include <iostream> using namespace std; char map[105][105]; int main( ) { int N, M; while( scanf( "%d %d", &N, &M )!= EOF ) { for( int i= 1; i<= N; ++i ) { for( int j= 1; j<= M; ++j ) { char c; while( c= getchar() ) { if( c== '0'|| c== '1' ) { map[i][j]= c; break; } } } } int ans= 0; if( M<= N ) { for( int i= 1; i< M; ++i ) { for( int j= i+ 1; j<= M; ++j ) { int cnt= 0; for( int k= 1; k<= N; ++k ) { if( map[k][i]== '1'&& map[k][j]== '1' ) { cnt++; } } ans+= ( cnt* ( cnt- 1 ) )/ 2; } } } else { for( int i= 1; i< N; ++i ) { for( int j= i+ 1; j<= N; ++j ) { int cnt= 0; for( int k= 1; k<= M; ++k ) { if( map[i][k]== '1'&& map[j][k]== '1' ) { cnt++; } } ans+= ( cnt* ( cnt- 1 ) )/ 2; } } } printf( "%d\n", ans ); } return 0; }