Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class t165 {
- static boolean[] color;
- static boolean[] used;
- static int n, m;
- static List<List<Integer>> g;
- static boolean ans = true;
- public static void dfs(int v) {
- used[v] = true;
- for (int to : g.get(v)) {
- if (!used[to]) {
- color[to] = !color[v];
- dfs(to);
- } else if (used[to] && color[to] == color[v]) {
- ans = false;
- }
- }
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- n = in.nextInt();
- m = in.nextInt();
- g = new ArrayList<>(n);
- color = new boolean[n];
- used = new boolean[n];
- for (int i = 0; i < n; ++i) {
- g.add(new ArrayList<>());
- }
- for (int i = 0; i < m; ++i) {
- int fr, to;
- fr = in.nextInt();
- to = in.nextInt();
- g.get(fr - 1).add(to - 1);
- g.get(to - 1).add(fr - 1);
- }
- for (int i = 0; i < n; ++i) {
- if (!used[i]) {
- dfs(i);
- }
- }
- if (ans) {
- System.out.println("YES");
- for (int i = 0; i < n; ++i) {
- if (!color[i]) {
- System.out.print(i + 1 + " ");
- }
- }
- } else {
- System.out.println("NO");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement