From e35a2552165e9fd3d733a35f97be9d9c4a3c8551 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 20 Feb 2025 11:15:36 +0900 Subject: [PATCH] =?UTF-8?q?[20250220]=20BOJ=20/=20=EA=B3=A8=EB=93=9C4=20/?= =?UTF-8?q?=20=ED=8D=BC=EB=A0=88=EC=9D=B4=EB=93=9C=20/=20=EA=B9=80?= =?UTF-8?q?=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...74\353\240\210\354\235\264\353\223\234.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "suyeun84/202502/20 BOJ G4 \355\215\274\353\240\210\354\235\264\353\223\234.md" diff --git "a/suyeun84/202502/20 BOJ G4 \355\215\274\353\240\210\354\235\264\353\223\234.md" "b/suyeun84/202502/20 BOJ G4 \355\215\274\353\240\210\354\235\264\353\223\234.md" new file mode 100644 index 00000000..595a36fc --- /dev/null +++ "b/suyeun84/202502/20 BOJ G4 \355\215\274\353\240\210\354\235\264\353\223\234.md" @@ -0,0 +1,61 @@ +```java +import java.util.*; +import java.io.*; + +public class Solution { + static ArrayList> graph; + static boolean[] visited; + static int V, E; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + V = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + graph = new ArrayList<>(V+1); + visited = new boolean[V+1]; + for (int i = 0; i< V+1; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < E; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + graph.get(a).add(b); + graph.get(b).add(a); + } + connected(1); + for (int i = 1; i < V+1; i++) { + if (!visited[i]) { + System.out.println("NO"); + return; + } + } + int odd = 0, even = 0; + for (int i = 1; i < V+1; i++) { + if (graph.get(i).size() % 2 == 1) odd++; + else even++; + } + if ((even == V-2 && odd == 2) || odd == 0) { + System.out.println("YES"); + } else { + System.out.println("NO"); + } + } + + static void connected(int idx) { + Queue q = new LinkedList<>(); + q.add(idx); + visited[idx] = true; + while (!q.isEmpty()) { + int curr = q.poll(); + for (int n : graph.get(curr)) { + if (visited[n]) continue; + visited[n] = true; + q.add(n); + } + } + } +} +```