Skip to content

Commit a88fb61

Browse files
committed
이분그래프 문제풀이 업데이트
1 parent e6ef357 commit a88fb61

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

part8/3.이분그래프.txt

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const fs = require("fs")
2+
let input = fs.readFileSync("index.txt").toString().split("\n")
3+
4+
class Queue {
5+
constructor() {
6+
this._arr = []
7+
}
8+
enqueue(item) {
9+
this._arr.push(item)
10+
}
11+
dequeue() {
12+
return this._arr.shift()
13+
}
14+
getLength() {
15+
return this._arr.length
16+
}
17+
}
18+
19+
20+
let testCases = Number(input.shift())
21+
22+
23+
// 빨간색 0,
24+
function bfs(x, graph, visited) {
25+
let queue = new Queue();
26+
queue.enqueue(x);
27+
28+
visited[x] = 0;
29+
30+
while (queue.getLength() != 0) {
31+
const out = queue.dequeue()
32+
33+
for (let el of graph[out]) {
34+
if (visited[el] === -1) {
35+
visited[el] = (visited[out] + 1) % 2
36+
queue.enqueue(el)
37+
}
38+
}
39+
40+
}
41+
42+
}
43+
44+
for (let i = 0; i < testCases; i++) {
45+
let [v, e] = input.shift().split(" ").map(Number);
46+
47+
let graph = Array.from(new Array(v + 1), () => []);
48+
49+
for (let j = 0; j < e; j++) {
50+
let [u, v] = input[j].split(" ").map(Number);
51+
graph[u].push(v);
52+
graph[v].push(u);
53+
}
54+
55+
let visited = new Array(v + 1).fill(-1);
56+
for (let i = 1; i <= v; i++) {
57+
if (visited[i] === -1) {
58+
bfs(i, graph, visited)
59+
}
60+
}
61+
62+
let answer = true
63+
for (let i = 1; i < visited.length; i++) {
64+
for (let el of graph[i]) {
65+
if (visited[i] == visited[el]) {
66+
answer = false
67+
}
68+
}
69+
}
70+
71+
72+
input = input.slice(e)
73+
console.log(answer?"YES":"NO")
74+
}
75+

0 commit comments

Comments
 (0)