From 6c62b1140284c7c5175c79bc0db54f15b16caa41 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sun, 14 Sep 2025 22:50:50 +0900 Subject: [PATCH] =?UTF-8?q?[20250914]=20BOJ=20/=20G4=20/=20=EC=9D=B4?= =?UTF-8?q?=EB=B6=84=20=EA=B7=B8=EB=9E=98=ED=94=84=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \352\267\270\353\236\230\355\224\204.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "lkhyun/202509/14 BOJ G4 \354\235\264\353\266\204 \352\267\270\353\236\230\355\224\204.md" diff --git "a/lkhyun/202509/14 BOJ G4 \354\235\264\353\266\204 \352\267\270\353\236\230\355\224\204.md" "b/lkhyun/202509/14 BOJ G4 \354\235\264\353\266\204 \352\267\270\353\236\230\355\224\204.md" new file mode 100644 index 00000000..37fa7c08 --- /dev/null +++ "b/lkhyun/202509/14 BOJ G4 \354\235\264\353\266\204 \352\267\270\353\236\230\355\224\204.md" @@ -0,0 +1,68 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int K; + static int V, E; + static List[] adjLists; + + public static void main(String[] args) throws IOException { + K = Integer.parseInt(br.readLine()); + + for (int i = 0; i < K; i++) { + st = new StringTokenizer(br.readLine()); + V = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + adjLists = new List[V + 1]; + for (int j = 1; j <= V; j++) { + adjLists[j] = new ArrayList<>(); + } + for (int j = 0; j < E; j++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + adjLists[u].add(v); + adjLists[v].add(u); + } + if (!BFS()) { + bw.write("NO\n"); + } else { + bw.write("YES\n"); + } + } + bw.close(); + } + + public static boolean BFS() { + ArrayDeque q = new ArrayDeque<>(); + int[] visited = new int[V+1]; + + for (int i = 1; i <= V; i++) { + if (visited[i] == 0) { + q.offer(i); + visited[i] = 1; + + while (!q.isEmpty()) { + int cur = q.poll(); + + for (int next : adjLists[cur]) { + if (visited[next] == 0) { + visited[next] = -visited[cur]; + q.offer(next); + } else if (visited[next] == visited[cur]) { + + return false; + } + } + } + } + } + return true; + } +} + +```