Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Seol-JY/202511/23 BOJ G4 이분 그래프.md‎
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
```java
import java.io.*;
import java.util.*;

public class Main {
static ArrayList<Integer>[] graph;
static int[] color;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

int K = Integer.parseInt(br.readLine());

while(K-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int V = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());

graph = new ArrayList[V + 1];
color = new int[V + 1];

for(int i = 1; i <= V; i++) {
graph[i] = new ArrayList<>();
}

for(int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
graph[u].add(v);
graph[v].add(u);
}

boolean isBipartite = true;
for(int i = 1; i <= V; i++) {
if(color[i] == 0) {
if(!bfs(i)) {
isBipartite = false;
break;
}
}
}

sb.append(isBipartite ? "YES" : "NO").append("\n");
}

System.out.print(sb);
}

static boolean bfs(int start) {
ArrayDeque<Integer> q = new ArrayDeque<>();
q.offer(start);
color[start] = 1;

while(!q.isEmpty()) {
int cur = q.poll();

for(int next : graph[cur]) {
if(color[next] == 0) {
color[next] = -color[cur];
q.offer(next);
} else if(color[next] == color[cur]) {
return false;
}
}
}

return true;
}
}

```