Problem at TCS

In a class room everyone is very friendly and has bonded with others in a short span of time. During the exams, students will sit along with their friends and will write the exams, which actually resulted in the finding that only a few members in the batch are good at studies and others are not. After getting several complaints from the staff members, the Principal has agreed to change the sitting pattern during the exams for which she has formed a committee. Using a spy, committee was able to get a list of close friends for all the students in the class. Now using this list they want to identify two groups of people such that a person in one group must not be a friend to any other in the same group. Your task is to help the committee.

Input Format:

Input consists of two parts, viz.

  1. First Line contains, number of students in the class room (N) and number of friendship connections (M)

  2. Next M line contains a list that represents two integers i and j, which represents that i and j are friends

Output Format:

Print “Yes” if the committee can divide the students in two groups of people, else print “No”.

Constraints:
1 <= N <= 50

1 <= M <= N * (N-1)/2

1 <= I, j <= N

pls, provide a best algorithm for these kind of problems.

I am thinking of a simple BFS solution for this problem. Firstly create the given graph using a adjacency list. Let us assume the set of nodes is partitioned into two sets 1 and 2. Start from a vertex with degree greater than 0. We assign this to set 1.Do a BFS from this node. Now all the children of this node(friends basically) will have to be in group 2 as the parent is in 1. Continuing this, children of these children need to be in set 1 and so on.

If at any point you find that a node needs to be assigned set 2(or 1) and it has already been assigned to the opposite set then a conflict arises and answer is “No”. If BFS(partitioning completes successfully) then answer is “Yes”. In terms of complexity this solution is O(N)

1 Like

sorry for deleting my answer…i thought the ques was about another problem of the same contest…@kcahdog’s answer is correct…u can find a similar problem here: spoj.com/problems/BUGLIFE

2 Likes

had used floyd-warshall for another problem of the same contest…got confused a bit…yup bfs it is!!!

@kunal361 which contest?

TCS codevita Round2!!

1 Like

I had participated in CodeVita last year and had spent more time on correct input output than on actual algorithm itself so did not participate this year. The questions look better this year.