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; + } +} + +```