Superty's blog

By Superty, 9 years ago, In English

It is the 5th problem here: http://hsin.hr/coci/contest4_tasks.pdf (SABOR)

  • Vote: I like it
  • +21
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +45 Vote: I do not like it

We can model the situation as an undirected graph in which the vertices are the members of Parliament and the edges connect the captured arguments.

One of the things we can conclude from the task statement is the following: each vertex has degree at most 5. (We can conclude a bit more, but it's unnecessary). Now we need to paint the vertices of this graph either white or black. However, no vertex can have more than two neighbours coloured in the same way as this vertex.

Now I state that we can paint all the vertices white and everything will be OK (that is, the answer is AAAA...A). Uhm... Of course that's not true. But why can it be wrong? It's incorrect if there is a vertex (call it 'v') which has at least three (or which is more important: more than a half of...) neighbours coloured the same way as v. So what happens if we flip the color of v? Yeah, we'll fix this property for v (now it has no more than two same-colored neighbours). But there may be other vertices... So do it again. And again. And again... Until there are no bad vertices.

However, you may say: Hey, but why the heck does this process always terminate? Yes, it's tricky (note that during the process you may find it necessary to flip some vertices more than once; suitable test is in the end of this comment). Now here comes the trick: count the edges connecting the vertices of the same color. Each fix decreases this number by at least one. Now the proof should be easy to complete (you can prove that this process terminates; and even more — terminates after no more than O(n) steps). Of course, it's also a proof that the solution always exists.

I also hope it's not hard to come up with an efficient method of processing bad vertices (hints which may be helpful in doing it the way I would to this: — queue containing bad vertices, — flipping the color of v can make neighbours of v bad, and only them). I believe that it's O(n) then.

And for better understanding — the test I mentioned:

7
3  2 1  3 6  4 7
3  3 1  4 6  2 7
3  4 1  2 6  3 5
2  3 7  2 5
1  4 5

As it may look quite bad, I explain: vertex 1 is connected with 2, 3, 4 and each vertex from the set {2,3,4} is connected with each vertex from {5,6,7}. At first, all vertices are white. Then we see that 1 is bad and paint it black. Then we do the same with vertices 2, 3, 4. However, we now notice that 1 became bad again — it means we must paint it white. The final answer is ABBBAAA.

Side note: the fact that all the vertices were initially painted white is completely unnecessary. We could for example paint them randomly (and who knows, maybe the program would run slightly faster then).

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks a lot for your detailed answer. I've got it accepted now.