Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.util.Pair;
- import java.io.*;
- import java.lang.reflect.Array;
- import java.util.*;
- public class Arsen {
- static ArrayList<Integer>[] gr;
- static boolean[] was;
- static int[] color;
- /*
- static int nextInt() throws IOException {
- while (!st.hasMoreTokens()) {
- st = new StringTokenizer(br.readLine());
- }
- return Integer.parseInt(st.nextToken());
- }
- static BufferedReader br;
- static StringTokenizer st = new StringTokenizer("");
- */
- static boolean dfs(int v, int c) {
- was[v] = true;
- color[v] = c;
- boolean ans = true;
- for (Integer to : gr[v]) {
- if (!was[to]) {
- ans &= dfs(to, 3 - c);
- } else {
- if (color[v] == color[to]) {
- return false;
- }
- }
- }
- return ans;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- PrintWriter out = new PrintWriter(System.out);
- int n = in.nextInt();
- int m = in.nextInt();
- gr = new ArrayList[n];
- was = new boolean[n];
- color = new int[n];
- for (int i = 0; i < n; i++) {
- gr[i] = new ArrayList<>();
- }
- for (int i = 0; i < m; i++) {
- int from = in.nextInt() - 1;
- int to = in.nextInt() - 1;
- gr[from].add(to);
- gr[to].add(from);
- }
- for (int i = 0; i < n; i++) {
- if (!was[i]) {
- if (!dfs(i, 1)) {
- out.print("NO");
- out.close();
- return;
- }
- }
- }
- out.println("YES");
- for (int i = 0; i < n; i++) {
- out.print(color[i] + " ");
- }
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement