Skip to content

Commit 8e492aa

Browse files
committed
81차 1번 문제풀이이
1 parent b69452c commit 8e492aa

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

live8/test81/문제1/황장현.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n')
6+
.map((el) => el.split(' ').map(Number));
7+
8+
function solution(input) {
9+
const [N, M, V] = input[0];
10+
const graph = Array.from(Array(N + 1), () => Array(N + 1).fill(0));
11+
12+
for (let i = 1; i <= M; i++) {
13+
let [row, column] = input[i];
14+
graph[row][column] = 1;
15+
graph[column][row] = 1;
16+
}
17+
18+
const visited = new Array(N + 1).fill(false);
19+
const dfs_answer = [];
20+
const bfs_answer = [];
21+
22+
function dfs(V) {
23+
visited[V] = true;
24+
dfs_answer.push(V);
25+
for (let i = 1; i < graph.length; i++) {
26+
if (graph[V][i] === 1 && !visited[i]) {
27+
dfs(i);
28+
}
29+
}
30+
}
31+
32+
function bfs(V) {
33+
const queue = [];
34+
visited[V] = true;
35+
bfs_answer.push(V);
36+
queue.push(V);
37+
38+
while (queue.length !== 0) {
39+
let dequeue = queue.shift();
40+
for (let i = 1; i < graph.length; i++) {
41+
if (graph[dequeue][i] === 1 && !visited[i]) {
42+
visited[i] = true;
43+
queue.push(i);
44+
bfs_answer.push(i);
45+
}
46+
}
47+
}
48+
}
49+
50+
dfs(V);
51+
visited.fill(false);
52+
bfs(V);
53+
54+
console.log(dfs_answer.join(' '));
55+
console.log(bfs_answer.join(' '));
56+
}
57+
58+
solution(input);

0 commit comments

Comments
 (0)