Skip to content

Commit 1a6745c

Browse files
author
Eric
committed
95차 2번 문제풀이
1 parent 3237829 commit 1a6745c

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

live9/test95/문제2/황장현.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function solution(n, wires) {
2+
const graph = Array.from({ length: n + 1 }, () => []);
3+
4+
for (let [w1, w2] of wires) {
5+
graph[w1].push(w2);
6+
graph[w2].push(w1);
7+
}
8+
9+
let min = Infinity;
10+
11+
for (let [cutA, cutB] of wires) {
12+
const visited = Array(n + 1).fill(false);
13+
14+
function DFS(node) {
15+
visited[node] = true;
16+
let count = 1;
17+
18+
for (const next of graph[node]) {
19+
if (!visited[next] && !(next === cutB)) {
20+
count += DFS(next);
21+
}
22+
}
23+
24+
return count;
25+
}
26+
27+
const countA = DFS(cutA);
28+
const countB = n - countA;
29+
30+
min = Math.min(min, Math.abs(countA - countB));
31+
}
32+
33+
return min;
34+
}
35+
36+
console.log(
37+
solution(9, [
38+
[1, 3],
39+
[2, 3],
40+
[3, 4],
41+
[4, 5],
42+
[4, 6],
43+
[4, 7],
44+
[7, 8],
45+
[7, 9],
46+
])
47+
);

0 commit comments

Comments
 (0)