|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Main{ |
| 8 | + private static int V; |
| 9 | + private static int E; |
| 10 | + private static List<List<Integer>> graph; |
| 11 | + private static boolean[] visited; |
| 12 | + private static int maxPassedVertex; |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + String[] temp; |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + temp = br.readLine().split(" "); |
| 17 | + V = Integer.parseInt(temp[0]); |
| 18 | + E = Integer.parseInt(temp[1]); |
| 19 | + |
| 20 | + graph = new ArrayList<>(); |
| 21 | + |
| 22 | + int[] ds = new int[V+1];//degreesAtVertex |
| 23 | + for(int i = 0; i < V+1; i++){ |
| 24 | + graph.add(new ArrayList<>() ); |
| 25 | + } |
| 26 | + for (int i = 0; i < E; i++) { |
| 27 | + temp = br.readLine().split(" "); |
| 28 | + int s = Integer.parseInt(temp[0]); |
| 29 | + int e = Integer.parseInt(temp[1]); |
| 30 | + ds[s]++; |
| 31 | + ds[e]++; |
| 32 | + graph.get(s).add(e); |
| 33 | + graph.get(e).add(s); |
| 34 | + } |
| 35 | + |
| 36 | + visited = new boolean[V+1]; |
| 37 | + visited[1] = true; |
| 38 | + dfs(1, 1); |
| 39 | + maxPassedVertex = 0; |
| 40 | + for(int i = 1; i < V+1; i++){ |
| 41 | + if(visited[i]) maxPassedVertex++; |
| 42 | + } |
| 43 | + if(maxPassedVertex != V){ |
| 44 | + //System.out.println(maxPassedVertex + " " + V); |
| 45 | + System.out.println("NO"); |
| 46 | + System.exit(0); |
| 47 | + } |
| 48 | + |
| 49 | + int odd = 0; |
| 50 | + for(int i = 1; i<V+1; i++){ |
| 51 | + if(ds[i] %2 == 1){ |
| 52 | + odd++; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + System.out.println((odd==0 || odd == 2) ?"YES":"NO"); |
| 57 | + br.close(); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + private static void dfs(int sv, int passedVertex) { |
| 62 | + for(int nv : graph.get(sv)){ |
| 63 | + if(visited[nv] == true ) |
| 64 | + continue; |
| 65 | + visited[nv] = true; |
| 66 | + dfs(nv, passedVertex+1); |
| 67 | + //visited[nv] = false; |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
0 commit comments