It is the 5th problem here: http://hsin.hr/coci/contest4_tasks.pdf (SABOR)
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
It is the 5th problem here: http://hsin.hr/coci/contest4_tasks.pdf (SABOR)
Name |
---|
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:
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).
Thanks a lot for your detailed answer. I've got it accepted now.