nownikhil's blog

By nownikhil, 11 years ago, In English

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2753

I have been trying to solve this UVA problem(11706 — Party Night), but am unable to find a solution to this. Can someone tell how to solve this problem.

Thanks in advance.

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

»
11 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

First note the following for any triplets of different people a,b,c...

  • If 'a' knows 'b' and 'a' knows 'c', but 'b' doesn't know 'c', then 'a' must meet 'b' and 'c' on two different parties.
  • If the three of them know each other, then they must all meet on the same party (because of the restriction that no two people can go to the same parties).

That information is enough to solve the problem. We'll do it by coloring the relationships with two different colors.

First of all, iterate through all possible triplets and check if one of the two cases apply. For every relationship, keep track of which relationships have to be of the same color and which ones have to be of the other color.

Then iterate through the relationships. If it hasn't been assigned any color yet, assign it some color and process it. The process is as follows...

Process(relationship r)
{
    For each relationship k marked as different than r
    {
        If it has already been assigned the same color
            The configuration is invalid, exit
        If it has already been assigned the other color
            Do nothing
        If it hasnt been assigned any color
        {
            Assign it the other color
            Process(k)
        }
    }
    
    For each relationship k marked as the same color as r
    {
        If it has already been assigned the same color
            Do nothing
        If it has already been assigned the other color
            Configuration is invalid, exit
        If it hasnt been assigned any color
        {
            Assign it the same color
            Process(k)
        }
    }
}

I got it accepted with this.