# \[99클럽 코테 스터디 36일차 TIL]  프로그래머스 - 전력망을 둘로 나누기

## \[level 2] 전력망을 둘로 나누기 - 86971

[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/86971)

#### 문제 설명

n개의 송전탑이 전선을 통해 하나의 [트리](https://en.wikipedia.org/wiki/Tree_\(data_structure\)) 형태로 연결되어 있습니다. 당신은 이 전선들 중 하나를 끊어서 현재의 전력망 네트워크를 2개로 분할하려고 합니다. 이때, 두 전력망이 갖게 되는 송전탑의 개수를 최대한 비슷하게 맞추고자 합니다.

송전탑의 개수 n, 그리고 전선 정보 wires가 매개변수로 주어집니다. 전선들 중 하나를 끊어서 송전탑 개수가 가능한 비슷하도록 두 전력망으로 나누었을 때, 두 전력망이 가지고 있는 송전탑 개수의 차이(절대값)를 return 하도록 solution 함수를 완성해주세요.

***

**제한사항**

* n은 2 이상 100 이하인 자연수입니다.
* wires는 길이가 `n-1`인 정수형 2차원 배열입니다.
  * wires의 각 원소는 \[v1, v2] 2개의 자연수로 이루어져 있으며, 이는 전력망의 v1번 송전탑과 v2번 송전탑이 전선으로 연결되어 있다는 것을 의미합니다.
  * 1 ≤ v1 < v2 ≤ n 입니다.
  * 전력망 네트워크가 하나의 트리 형태가 아닌 경우는 입력으로 주어지지 않습니다.

***

**입출력 예**

| n | wires                                               | result |
| - | --------------------------------------------------- | ------ |
| 9 | `[[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]]` | 3      |
| 4 | `[[1,2],[2,3],[3,4]]`                               | 0      |
| 7 | `[[1,2],[2,7],[3,7],[3,4],[4,5],[6,7]]`             | 1      |

***

**입출력 예 설명**

입출력 예 #1

* 다음 그림은 주어진 입력을 해결하는 방법 중 하나를 나타낸 것입니다.
* ![ex1.png](https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/5b8a0dcd-cba0-47ca-b5e3-d3bafc81f9d6/ex1.png)
* 4번과 7번을 연결하는 전선을 끊으면 두 전력망은 각 6개와 3개의 송전탑을 가지며, 이보다 더 비슷한 개수로 전력망을 나눌 수 없습니다.
* 또 다른 방법으로는 3번과 4번을 연결하는 전선을 끊어도 최선의 정답을 도출할 수 있습니다.

입출력 예 #2

* 다음 그림은 주어진 입력을 해결하는 방법을 나타낸 것입니다.
* ![ex2.png](https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/b28865e1-a18e-429d-ae7a-14e77e801539/ex2.png)
* 2번과 3번을 연결하는 전선을 끊으면 두 전력망이 모두 2개의 송전탑을 가지게 되며, 이 방법이 최선입니다.

입출력 예 #3

* 다음 그림은 주어진 입력을 해결하는 방법을 나타낸 것입니다.
* ![ex3.png](https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/0a7f21af-1e07-4015-8ad3-c06155c613b3/ex3.png)
* 3번과 7번을 연결하는 전선을 끊으면 두 전력망이 각각 4개와 3개의 송전탑을 가지게 되며, 이 방법이 최선입니다.

***

## 풀이 코드

```java
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    static int[][] graph;
    
    public int solution(int n, int[][] wires) {
        int answer = n;
        graph = new int[n+1][n+1];
        
        for(int i=0; i<wires.length; i++){
            int from = wires[i][0], to = wires[i][1];
            graph[from][to] = 1;
            graph[to][from] = 1;
        }
        
        for(int i=0; i<wires.length; i++){
            int from = wires[i][0], to = wires[i][1];
            graph[from][to] = 0;
            graph[to][from] = 0;
            
            answer = Math.min(answer, bfs(n, from));
            graph[from][to] = 1;
            graph[to][from] = 1;
            
        }
        return answer;
    }
    
    public static int bfs(int n, int x){
        boolean[] visited = new boolean[n+1];
        int count = 1;
        
        Queue<Integer> que = new LinkedList<Integer>();
        visited[x] = true;
        que.offer(x);
        
        while(!que.isEmpty()){
            int temp = que.poll();
            
            for(int i=1; i<=n; i++){
                if(graph[temp][i] == 1 && !visited[i]){
                    visited[i] = true;
                    que.offer(i);
                    count ++;
                }
            }
        }
        
        return (int)Math.abs(count-(n-count));
    }
}
```

* Time: 21.69 ms
* Memory: 70 MB

양방향 그래프와 BFS를 통해 풀 수 있는 문제였다.

***

\#99클럽 #코딩테스트준비 #개발자취업 #항해99 #TIL


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devlee.gitbook.io/docs/study/99/99-36-til.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
