Skip to content

Commit 6673e13

Browse files
authored
Merge pull request #821 from AlgorithmWithGod/Ukj0ng
[20250905] BOJ / G4 / 이분 그래프 / 한종욱
2 parents 8f0ef71 + dc997b2 commit 6673e13

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static List<Integer>[] graph;
9+
private static int[] visited;
10+
private static int V, E;
11+
12+
public static void main(String[] args) throws IOException {
13+
int K = Integer.parseInt(br.readLine());
14+
15+
while (K-- > 0) {
16+
init();
17+
18+
boolean result = true;
19+
20+
for (int i = 1; i <= V; i++) {
21+
if (visited[i] == 0) {
22+
result = BFS(i);
23+
}
24+
25+
if (!result) break;
26+
}
27+
28+
if (result) bw.write("YES" + "\n");
29+
else bw.write("NO" + "\n");
30+
}
31+
32+
bw.flush();
33+
bw.close();
34+
br.close();
35+
}
36+
37+
private static void init() throws IOException {
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
V = Integer.parseInt(st.nextToken());
40+
E = Integer.parseInt(st.nextToken());
41+
42+
visited = new int[V + 1];
43+
graph = new List[V + 1];
44+
45+
for (int i = 0; i <= V; i++) {
46+
graph[i] = new ArrayList<>();
47+
}
48+
49+
for (int i = 0; i < E; i++) {
50+
st = new StringTokenizer(br.readLine());
51+
52+
int a = Integer.parseInt(st.nextToken());
53+
int b = Integer.parseInt(st.nextToken());
54+
55+
graph[a].add(b);
56+
graph[b].add(a);
57+
}
58+
}
59+
60+
private static boolean BFS(int start) {
61+
Queue<Integer> q = new ArrayDeque<>();
62+
visited[start] = 1;
63+
q.add(start);
64+
65+
while (!q.isEmpty()) {
66+
int current = q.poll();
67+
68+
for (int next : graph[current]) {
69+
if (visited[next] == visited[current]) return false;
70+
71+
if (visited[next] == 0) {
72+
visited[next] = 3 - visited[current];
73+
q.add(next);
74+
}
75+
}
76+
}
77+
78+
return true;
79+
}
80+
}
81+
```

0 commit comments

Comments
 (0)