import java.util.*;
import java.io.*;

public class Main {
    static List<List<Integer>> graph; 
    static int[] visited;
    
    public static boolean bfs(int v) {
        Queue<Integer> q = new ArrayDeque<>();
        q.add(v); 
        visited[v] = 0; 
        while (!(q.isEmpty())){
            v = q.poll();
            for (int u : graph.get(v)) {
                if (visited[u] == visited[v]) {
                    return false;
                }
                
                if (visited[u] == -1) {
                    visited[u] = (visited[v] == 0) ? 1: 0;
                    q.add(u);
                }
            }
        }
        
        return true;

        
    }
    
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        
        graph = new ArrayList<>();

        for (int i=0; i<N; i++) {
            graph.add(new ArrayList<>());
        }
        
        for (int i=0; i<M; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken())-1;
            int b = Integer.parseInt(st.nextToken())-1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        
        for (List<Integer> g : graph) {
            Collections.sort(g);
        }
        
        visited = new int[N];
        Arrays.fill(visited, -1);
        boolean isBipart = true; 
        for (int i=0; i<N; i++) {
            if (visited[i]==-1) {
                if (!bfs(i)) isBipart = false; 
                break;
            }
        }
        
        System.out.println(isBipart? "Yes" : "No");
        
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: