From 5295a1027c6c2a8278eae865794852ee8ab65b6c Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Tue, 5 Aug 2025 07:54:20 +0900 Subject: [PATCH] =?UTF-8?q?[20250805]=20BOJ=20/=20G5=20/=20ABCDE=20/=20?= =?UTF-8?q?=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JHLEE325/202508/05 BOJ G5 ABCDE.md | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 JHLEE325/202508/05 BOJ G5 ABCDE.md diff --git a/JHLEE325/202508/05 BOJ G5 ABCDE.md b/JHLEE325/202508/05 BOJ G5 ABCDE.md new file mode 100644 index 00000000..9637a0e3 --- /dev/null +++ b/JHLEE325/202508/05 BOJ G5 ABCDE.md @@ -0,0 +1,57 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static int n, m; + static List[] friends; + static boolean[] visited; + static boolean found = false; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + + friends = new ArrayList[n]; + for (int i = 0; i < n; i++) friends[i] = new ArrayList<>(); + + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + friends[a].add(b); + friends[b].add(a); + } + + visited = new boolean[n]; + for (int i = 0; i < n && !found; i++) { + visited[i] = true; + dfs(i, 1); + visited[i] = false; + } + + System.out.println(found ? 1 : 0); + } + + static void dfs(int cur, int depth) { + if (found) return; + if (depth == 5) { + found = true; + return; + } + + for (int next : friends[cur]) { + if (visited[next]) continue; + visited[next] = true; + dfs(next, depth + 1); + visited[next] = false; + if (found) return; + } + } +} + +```