Harry Potter and the Vector Spell
InterviewTime limit1sMemory limit256 MB
Each of N columns is a binary vector with exactly two 1s; find the rank of this M-by-N matrix over GF(2).
- Level
Medium6 of 10
- Topics
- Graph, Union-find, Math, Greedy
- Solved
- No attempts yet
Problem
Harry Potter found one more strange spell in the Half Blood Prince's diary. The spell creates a binary vector of size . Harry is not the best magician yet, so the spell does not work perfectly and every vector it creates has exactly 2 non-zero elements.
Harry used the spell times and stood all of the created vectors up as columns, which gives a matrix with rows and columns.
In the Magical Matrix Theory class the professor asked Harry to compute the rank of that matrix. Help him.
Operations in Magical Matrix Theory follow these rules.
The rank of a matrix is the largest number of linearly independent columns of . The vectors in a set are linearly independent when the equation , where , holds only for .
Input
The first line has two integers (the size of a vector) and (the number of vectors Harry created).
Each of the next lines has the format , , , , . Here is the number of non-zero elements in row , and the next numbers are the indices of the columns that are non-zero in that row (, ).
- Every column has exactly 2 non-zero elements.
Output
Print the rank of the matrix on the first line.
Hint
In the first example Harry created three vectors.
The matrix is
but .