From 04e65f54643f15d17169293a427b8013359b88d1 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Wed, 17 Dec 2025 15:17:44 +0900 Subject: [PATCH] =?UTF-8?q?[20251217]=20BOJ=20/=20G5=20/=20=EC=83=89?= =?UTF-8?q?=EC=B9=A0=ED=95=98=EA=B8=B0=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\354\271\240\355\225\230\352\270\260.md" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "JHLEE325/202512/17 BOJ G5 \354\203\211\354\271\240\355\225\230\352\270\260.md" diff --git "a/JHLEE325/202512/17 BOJ G5 \354\203\211\354\271\240\355\225\230\352\270\260.md" "b/JHLEE325/202512/17 BOJ G5 \354\203\211\354\271\240\355\225\230\352\270\260.md" new file mode 100644 index 00000000..681d8907 --- /dev/null +++ "b/JHLEE325/202512/17 BOJ G5 \354\203\211\354\271\240\355\225\230\352\270\260.md" @@ -0,0 +1,74 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static ArrayList[] adj; + static int[] colors; + static boolean isPossible; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + int T = Integer.parseInt(br.readLine()); + + while (T-- > 0) { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + adj = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) { + adj[i] = new ArrayList<>(); + } + colors = new int[N + 1]; + isPossible = true; + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + + adj[u].add(v); + adj[v].add(u); + } + + for (int i = 1; i <= N; i++) { + if (!isPossible) break; + + if (colors[i] == 0) { + bfs(i); + } + } + + if (isPossible) { + System.out.println("possible"); + } else { + System.out.println("impossible"); + } + } + } + + private static void bfs(int startNode) { + Queue queue = new LinkedList<>(); + queue.add(startNode); + colors[startNode] = 1; + + while (!queue.isEmpty()) { + int current = queue.poll(); + + for (int next : adj[current]) { + if (colors[next] == 0) { + colors[next] = 3 - colors[current]; + queue.add(next); + } + else if (colors[next] == colors[current]) { + isPossible = false; + return; + } + } + } + } +}```