HUT-1558 Count Cycles

1558: Count Cycles

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 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

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

Sample Output

0
0
2

  这题是要我们找出所给矩阵中有多少个四个点都为1的矩阵。思路是这样的,首先选取行列中较小的一个,然后定义两个指针进行组合,以第三个数据进行说明,这里行列相等,就假设选取了列,那么首先第一列和第二列组合,看这两列下面的有多少行满足有都为1的两点,统计完之后,那么最和的矩阵数就是一个1+2+3+... 的问题了,后面再1/3列组合,2/3列组合。
  代码如下:
#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;
}
posted @ 2011-08-02 19:22  沐阳  阅读(274)  评论(0编辑  收藏  举报