diff --git "a/lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" "b/lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" new file mode 100644 index 00000000..45362f48 --- /dev/null +++ "b/lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" @@ -0,0 +1,42 @@ +```java +import java.util.*; +class Solution { + static List[] adjList; + public int solution(int n, int[][] wires) { + int answer = n; + adjList = new List[n+1]; + for(int i = 0; i<=n; i++){ + adjList[i] = new ArrayList<>(); + } + for(int[] wire : wires){ + adjList[wire[0]].add(wire[1]); + adjList[wire[1]].add(wire[0]); + } + for(int[] wire : wires){ + answer = Math.min(answer,Math.abs(n-(2*BFS(n, wire[0],wire[1])))); + } + + return answer; + } + public int BFS(int n, int start, int except){ + int count = 0; + ArrayDeque q = new ArrayDeque<>(); + boolean[] visited = new boolean[n+1]; + q.offer(start); + visited[start] = true; + count++; + + while(!q.isEmpty()){ + int cur = q.poll(); + + for(int next : adjList[cur]){ + if(visited[next] || next == except) continue; + q.offer(next); + visited[next] = true; + count++; + } + } + return count; + } +} +```