Skip to content

Commit a77f199

Browse files
authored
[20251123] BOJ / G4 / 이분 그래프 / 이종환
1 parent 92e852c commit a77f199

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.HashSet;
10+
import java.util.LinkedList;
11+
import java.util.PriorityQueue;
12+
import java.util.Queue;
13+
import java.util.StringTokenizer;
14+
15+
public class Main {
16+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
static StringBuilder sb = new StringBuilder();
18+
static int nodeCnt, edgeCnt;
19+
static String ans;
20+
static Node[] nodes;
21+
static boolean[] visited;
22+
23+
static class Node{
24+
int num;
25+
int team = 0;
26+
HashSet<Node> set = new HashSet<>();
27+
public Node(int num) {
28+
this.num = num;
29+
}
30+
}
31+
32+
33+
public static void main(String[] args) throws IOException {
34+
int tc = Integer.parseInt(br.readLine());
35+
for (int i = 0; i < tc; i++) {
36+
init();
37+
process();
38+
sb.append(ans).append("\n");
39+
}
40+
print();
41+
42+
}
43+
44+
private static void init() throws IOException{
45+
StringTokenizer st = new StringTokenizer(br.readLine());
46+
nodeCnt = Integer.parseInt(st.nextToken());
47+
edgeCnt = Integer.parseInt(st.nextToken());
48+
49+
nodes = new Node[nodeCnt+1];
50+
visited = new boolean[nodeCnt+1];
51+
52+
for (int i = 1; i <= nodeCnt; i++) {
53+
nodes[i] = new Node(i);
54+
}
55+
for (int i = 0; i < edgeCnt; i++) {
56+
st = new StringTokenizer(br.readLine());
57+
Node from = nodes[Integer.parseInt(st.nextToken())];
58+
Node to = nodes[Integer.parseInt(st.nextToken())];
59+
from.set.add(to);
60+
to.set.add(from);
61+
}
62+
63+
}
64+
65+
private static void process() throws IOException {
66+
ans = "YES";
67+
for (int i = 1; i <= nodeCnt; i++) {
68+
Node target = nodes[i];
69+
if (visited[target.num]) continue;
70+
processRound(target.num);
71+
72+
73+
}
74+
75+
76+
77+
78+
}
79+
80+
private static void processRound(int idx) {
81+
nodes[idx].team = idx;
82+
Queue<Node> q = new LinkedList<>();
83+
q.add(nodes[idx]);
84+
while(!q.isEmpty()) {
85+
Node n = q.poll();
86+
if (visited[n.num]) continue;
87+
visited[n.num] = true;
88+
89+
int opp = n.team*(-1);
90+
91+
for (Node node: n.set) {
92+
if(node.team == 0) {
93+
node.team = opp;
94+
q.add(node);
95+
} else if (node.team == opp) {
96+
continue;
97+
}else {
98+
ans = "NO";
99+
return;
100+
}
101+
102+
103+
}
104+
105+
}
106+
107+
}
108+
109+
110+
111+
private static void print() {
112+
System.out.print(sb.toString());
113+
}
114+
115+
}
116+
```

0 commit comments

Comments
 (0)