1558: Count Cycles
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 84 Solved: 21 [ ][ ][ ]Description
In , a low-density parity-check (LDPC) code is a , a method of transmitting a message over a transmission channel, and is constructed using a sparse . LDPC codes are , which means that practical constructions exist that allow the noise threshold to be set very close (or even arbitrarily close on the ) to the theoretical maximum (the ) for a symmetric memory-less channel.
LDPC codes are defined by a sparse . This is often randomly generated and the elements in it are 0 or 1. If we want use LDPC codes, we should make the 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#include #include 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;}