Dsa
Graphs Dfs
Easy
Coloring a Graph
You have a graph represented as a list of edges. Your task is to determine if the graph can be colored using two colors such that no two adjacent nodes share the same color. Print 'Yes' if it can be colored this way, otherwise print 'No'.
Input Format:
- The first line contains an integer n, the number of nodes (1 <= n <= 1000).
- The second line contains an integer m, the number of edges.
- The next m lines each contain two integers a and b, representing an edge between node a and node b.
Output Format:
- 'Yes' or 'No'.
Example:
Input:
4
4
1 2
1 3
2 4
3 4
Output:
No
Key concepts
graphsdfsgraph coloring
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now