It is the 5th problem here: http://hsin.hr/coci/contest4_tasks.pdf (SABOR)
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.