Author(s) François Aubry
Deadline No deadline
Submission limit No limitation

Sign in

Graphs - Problem: Cross Bracing

This is a problem from the UCL Algorithm Contest Round 1 - 2015.

Sometimes a problem can be translated into a fairly simple graph problem but it is not obvious at all how to do so.

This problem is one such example. Note that in a real contest it would be harder as no one would give you a tip to try to see it as a graph problem.

Cross Bracing

Many engineering structures are constructed using triangles. Why is this?


The simple answer is that triangles are stable. A triangle has three sides and, if these are fixed in length, there is only one configuration they can be in. There is no flexibility or freedom. On the other hand, a quadrilateral has more degrees of freedom. Without changing any of the lengths of the sides the shape can be deformed.


Triangles help to keep structures rigid. So, to make a square rigid, we can use a cross section sub-dividing it into two triangles as shown in the figure.


In this problem we are interested to know if a given grid structure is rigid or not. A grid structure is rigid if no square can be deformed. For instance the following grid structure is not rigid as it can be deformed while maintaining the lengths of all non-crossed squares and the integrity of the crossed squares.


By adding a cross section on the lower left square, we obtain a rigid structure.


Given a grid with some cross sections, determine what is the minimum number of cross sections that must be added so that it becomes rigid.


The first line contains two integers \(r\) and \(c\) giving the number of rows and columns of the grid structure. Then follow \(r\) lines each containing a binary string \(s_i\) of length \(c\) such that \(s_{ij} = 1\) if and only if square at row \(i\) and column \(j\) is braced.


  • \(1 \leq r, c \leq 1000\).


A single line with a non-negative integer giving the minimum number of cross sections that must be added in order to make the structure rigid.

Sample Test Cases

Sample input 1

Sample output 1

Sample input 2

Sample output 2

Max file size: 1.0 MiB
Allowed extensions: .java, .cpp, .py