|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Solution { |
| 6 | + static ArrayList<ArrayList<Integer>> graph; |
| 7 | + static boolean[] visited; |
| 8 | + static int V, E; |
| 9 | + public static void main(String[] args) throws IOException { |
| 10 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 12 | + |
| 13 | + V = Integer.parseInt(st.nextToken()); |
| 14 | + E = Integer.parseInt(st.nextToken()); |
| 15 | + graph = new ArrayList<>(V+1); |
| 16 | + visited = new boolean[V+1]; |
| 17 | + for (int i = 0; i< V+1; i++) { |
| 18 | + graph.add(new ArrayList<>()); |
| 19 | + } |
| 20 | + |
| 21 | + for (int i = 0; i < E; i++) { |
| 22 | + st = new StringTokenizer(br.readLine()); |
| 23 | + int a = Integer.parseInt(st.nextToken()); |
| 24 | + int b = Integer.parseInt(st.nextToken()); |
| 25 | + graph.get(a).add(b); |
| 26 | + graph.get(b).add(a); |
| 27 | + } |
| 28 | + connected(1); |
| 29 | + for (int i = 1; i < V+1; i++) { |
| 30 | + if (!visited[i]) { |
| 31 | + System.out.println("NO"); |
| 32 | + return; |
| 33 | + } |
| 34 | + } |
| 35 | + int odd = 0, even = 0; |
| 36 | + for (int i = 1; i < V+1; i++) { |
| 37 | + if (graph.get(i).size() % 2 == 1) odd++; |
| 38 | + else even++; |
| 39 | + } |
| 40 | + if ((even == V-2 && odd == 2) || odd == 0) { |
| 41 | + System.out.println("YES"); |
| 42 | + } else { |
| 43 | + System.out.println("NO"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static void connected(int idx) { |
| 48 | + Queue<Integer> q = new LinkedList<>(); |
| 49 | + q.add(idx); |
| 50 | + visited[idx] = true; |
| 51 | + while (!q.isEmpty()) { |
| 52 | + int curr = q.poll(); |
| 53 | + for (int n : graph.get(curr)) { |
| 54 | + if (visited[n]) continue; |
| 55 | + visited[n] = true; |
| 56 | + q.add(n); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
0 commit comments