IPSC Logo

Internet Problem Solving Contest

IPSC 2003

Solution to Problem E – Enchanted Forest

The forest is represented by a 0,1-matrix. We will call the values 0 and 1 colors, 0 being white and 1 black. Instead of selecting a tree that has to be enchanted we select the corresponding element of our matrix. It is simple to divide the matrix into maximal connected one-colored areas. The spell performs a flood-fill starting at the selected element – the whole area containing the selected element changes color (from white to black or vice versa).

Let's construct a graph Gm = (V,E) where vertices will represent the areas in the matrix. Two vertices will be connected by an edge iff the corresponding two areas are adjacent (e.g. one of them contains an element with the coordinates (a,b) and the other an element (c,d) such that |a-c|+|b-d|=1). With each vertex we store its color. Note that all neighbors of a white vertex are black and vice versa.

What will happen with our graph after casting the spell? The selected area will change its color and merge with all neighboring areas into one larger area. Or, speaking in graph terms, we join the selected vertex and all its neighbors into one "large" vertex. Its color will be the opposite of the color of the selected vertex. We may throw away all the loops (previously edges between the selected vertex and its neighbors). We will call this operation a contraction of the vertex v. Note that also after any contractions all neighbors of a white vertex are black and vice versa. Our goal is to create a graph with only one vertex using as few contractions as possible.

Let G be an undirected graph. We start by defining some terms:

Lemma 1: After a contraction of a central point c the radius r of the graph decreases (at least by 1).

Proof: For each neighbor v of c we have d(v,c) = 1. Consider a vertex u != c, take any shortest u-c path. It leads through exactly one neighbor v of c. Therefore d(u,c) = d(u,v) + d(v,c) = d(u,v) + 1. It follows that the eccentricity of c' after the contraction will be one smaller than the eccentricity of c before the contraction. Let r' be the radius of the contracted graph. We know that r'<=ex(c') and ex(c')<ex(c)=r, q.e.d.

Lemma 2: Let c be any central point. After a contraction of any vertex v in V the eccentricity of c stays the same or decreases by 1.

Proof: Suppose v=c. We already proved that after a contraction of c its eccentricity decreases by one. Now suppose v!=c. Proof by contradiction. Clearly the eccentricity of c may not increase, therefore suppose it decreases at least by two. For each vertex u we take an arbitrary shortest c-u path Pu. As c is a central point, the length of some Pu is r. We will now find a vertex b such that ex(b) was less than r before the contraction. This will be a contradiction, because the eccentricity of c was the smallest one.

We assumed that after the contraction ex(c)<=r-2. Consider the paths Pu. Some of them were already shorter than r-1 before the contraction, we won't be interested in these. At least one of them had length r. Each Pu of length r is now shorter at least by two. It follows that either this Pu passes through v or through at least three neighbors of v. Without loss of generality (why?) we may assume the first case. Each Pu of length r-1 is now shorter at least by one. It follows that either this Pu passes through v or through at least two its neighbors.

Let b be the first vertex on some shortest c-v path (a neighbor of c). We will show that (in the graph before the contraction) ex(b)<r. Let u be an arbitrary vertex. We consider the following cases:

We have reached a contradiction – ex(b)<r but then c is not a central point. Therefore our assumption was wrong and the lemma is proved.

Theorem: The least number of contractions needed to contract the whole graph into a single vertex is r.

Proof: Follows from the above lemmas. Let c be any central point. After arbitrary r-1 contractions ex(c)>0 and therefore the graph has at least two vertices. On the other hand r contractions suffice – e.g. always contract the vertex c.

The algorithm is now pretty straightforward – we have to transform the given matrix to a graph and find its central point. The transformation can be easily done in O(RC) time. Then we will compute the eccentricity of each vertex by running a BFS (breadth first search) from each vertex. The time complexity of this part is O(N(M+N)), where N is the number of vertices and M is the number of edges of the graph. Note that the graph is planar, therefore M=O(N). The resulting time complexity is therefore O(RC+N2). (In the worst case this is O(R2C2).)