Advertisement
vov44k

t165

Feb 1st, 2023 (edited)
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class t165 {
  6.     static boolean[] color;
  7.     static boolean[] used;
  8.     static int n, m;
  9.     static List<List<Integer>> g;
  10.     static boolean ans = true;
  11.  
  12.     public static void dfs(int v) {
  13.         used[v] = true;
  14.  
  15.         for (int to : g.get(v)) {
  16.             if (!used[to]) {
  17.                 color[to] = !color[v];
  18.                 dfs(to);
  19.             } else if (used[to] && color[to] == color[v]) {
  20.                 ans = false;
  21.             }
  22.         }
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         Scanner in = new Scanner(System.in);
  27.         n = in.nextInt();
  28.         m = in.nextInt();
  29.         g = new ArrayList<>(n);
  30.         color = new boolean[n];
  31.         used = new boolean[n];
  32.         for (int i = 0; i < n; ++i) {
  33.             g.add(new ArrayList<>());
  34.         }
  35.         for (int i = 0; i < m; ++i) {
  36.             int fr, to;
  37.             fr = in.nextInt();
  38.             to = in.nextInt();
  39.             g.get(fr - 1).add(to - 1);
  40.             g.get(to - 1).add(fr - 1);
  41.         }
  42.         for (int i = 0; i < n; ++i) {
  43.             if (!used[i]) {
  44.                 dfs(i);
  45.             }
  46.         }
  47.         if (ans) {
  48.             System.out.println("YES");
  49.             for (int i = 0; i < n; ++i) {
  50.                 if (!color[i]) {
  51.                     System.out.print(i + 1 + " ");
  52.                 }
  53.             }
  54.         } else {
  55.             System.out.println("NO");
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement