Colour The Graph
You are given a graph with 'N' vertices numbered from '1' to 'N' and 'M' edges. You have to colour this graph in two different colours, say blue and red such that no two vertices connected by an edge are of the same colour.
Note :
The given graph may have connected components.
Input Format :
The first line of input contains two integers 'V' and 'E', separated by a single space. They denote the total number of vertices and edges respectively.
From the second line onwards, the next 'E' lines represent an edge between the two vertices.
Every edge is represented by two vertices(u, v) that share an edge between them. The values of the vertices would again be separated by a single space.
Output Format :
Print "YES" if it is possible else print "NO".
Constraints :
1 <= E <= 10 ^ 5
1 <= V <= 10 ^ 6
Time Limit: 1 sec
AnswerBot
1y
The problem is to color a graph with two colors such that no two connected vertices have the same color.
Use a graph coloring algorithm such as Depth First Search (DFS) or Breadth First Search (BFS).
St...read more
CodingNinjas
author
2y
DFS
We can approach this problem by running a DFS starting from vertex-1.
We have a total of ‘m’ edges.
When can we achieve the configuration when we see in reference to vertex-1?
- Let's say vertex-1 is ‘...read more
Help your peers!
Add answer anonymously...
Top Walmart Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app