Skip to content

Commit 4cd761c

Browse files
committed
[level 2] Title: 전력망을 둘로 나누기, Time: 6.72 ms, Memory: 75 MB -BaekjoonHub
1 parent 4491d2c commit 4cd761c

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [level 2] 전력망을 둘로 나누기 - 86971
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/86971)
4+
5+
### 성능 요약
6+
7+
메모리: 75 MB, 시간: 6.72 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 완전탐색
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 09월 04일 17:56:32
20+
21+
### 문제 설명
22+
23+
<p>n개의 송전탑이 전선을 통해 하나의 <a href="https://en.wikipedia.org/wiki/Tree_(data_structure)" target="_blank" rel="noopener">트리</a> 형태로 연결되어 있습니다. 당신은 이 전선들 중 하나를 끊어서 현재의 전력망 네트워크를 2개로 분할하려고 합니다. 이때, 두 전력망이 갖게 되는 송전탑의 개수를 최대한 비슷하게 맞추고자 합니다.</p>
24+
25+
<p>송전탑의 개수 n, 그리고 전선 정보 wires가 매개변수로 주어집니다. 전선들 중 하나를 끊어서 송전탑 개수가 가능한 비슷하도록 두 전력망으로 나누었을 때, 두 전력망이 가지고 있는 송전탑 개수의 차이(절대값)를 return 하도록 solution 함수를 완성해주세요.</p>
26+
27+
<hr>
28+
29+
<h5>제한사항</h5>
30+
31+
<ul>
32+
<li>n은 2 이상 100 이하인 자연수입니다.</li>
33+
<li>wires는 길이가 <code>n-1</code>인 정수형 2차원 배열입니다.
34+
35+
<ul>
36+
<li>wires의 각 원소는 [v1, v2] 2개의 자연수로 이루어져 있으며, 이는 전력망의 v1번 송전탑과 v2번 송전탑이 전선으로 연결되어 있다는 것을 의미합니다.</li>
37+
<li>1 ≤ v1 &lt; v2 ≤ n 입니다.</li>
38+
<li>전력망 네트워크가 하나의 트리 형태가 아닌 경우는 입력으로 주어지지 않습니다.</li>
39+
</ul></li>
40+
</ul>
41+
42+
<hr>
43+
44+
<h5>입출력 예</h5>
45+
<table class="table">
46+
<thead><tr>
47+
<th>n</th>
48+
<th>wires</th>
49+
<th>result</th>
50+
</tr>
51+
</thead>
52+
<tbody><tr>
53+
<td>9</td>
54+
<td><code>[[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]]</code></td>
55+
<td>3</td>
56+
</tr>
57+
<tr>
58+
<td>4</td>
59+
<td><code>[[1,2],[2,3],[3,4]]</code></td>
60+
<td>0</td>
61+
</tr>
62+
<tr>
63+
<td>7</td>
64+
<td><code>[[1,2],[2,7],[3,7],[3,4],[4,5],[6,7]]</code></td>
65+
<td>1</td>
66+
</tr>
67+
</tbody>
68+
</table>
69+
<hr>
70+
71+
<h5>입출력 예 설명</h5>
72+
73+
<p>입출력 예 #1</p>
74+
75+
<ul>
76+
<li>다음 그림은 주어진 입력을 해결하는 방법 중 하나를 나타낸 것입니다.</li>
77+
<li><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/5b8a0dcd-cba0-47ca-b5e3-d3bafc81f9d6/ex1.png" title="" alt="ex1.png"></li>
78+
<li>4번과 7번을 연결하는 전선을 끊으면 두 전력망은 각 6개와 3개의 송전탑을 가지며, 이보다 더 비슷한 개수로 전력망을 나눌 수 없습니다.</li>
79+
<li>또 다른 방법으로는 3번과 4번을 연결하는 전선을 끊어도 최선의 정답을 도출할 수 있습니다.</li>
80+
</ul>
81+
82+
<p>입출력 예 #2</p>
83+
84+
<ul>
85+
<li>다음 그림은 주어진 입력을 해결하는 방법을 나타낸 것입니다.</li>
86+
<li><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/b28865e1-a18e-429d-ae7a-14e77e801539/ex2.png" title="" alt="ex2.png"></li>
87+
<li>2번과 3번을 연결하는 전선을 끊으면 두 전력망이 모두 2개의 송전탑을 가지게 되며, 이 방법이 최선입니다.</li>
88+
</ul>
89+
90+
<p>입출력 예 #3</p>
91+
92+
<ul>
93+
<li>다음 그림은 주어진 입력을 해결하는 방법을 나타낸 것입니다.</li>
94+
<li><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/0a7f21af-1e07-4015-8ad3-c06155c613b3/ex3.png" title="" alt="ex3.png"></li>
95+
<li>3번과 7번을 연결하는 전선을 끊으면 두 전력망이 각각 4개와 3개의 송전탑을 가지게 되며, 이 방법이 최선입니다.</li>
96+
</ul>
97+
98+
99+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
class Solution {
3+
static List<Integer>[] edges;
4+
static int n, res;
5+
public int solution(int a, int[][] wires) {
6+
res = Integer.MAX_VALUE;
7+
n = a;
8+
edges = new List[n+1];
9+
for(int i=1; i<=n; i++) {
10+
edges[i] = new ArrayList<>();
11+
}
12+
for(int[] arr : wires) {
13+
edges[arr[0]].add(arr[1]);
14+
edges[arr[1]].add(arr[0]);
15+
}
16+
17+
for(int[] arr : wires) {
18+
int s = arr[0];
19+
int e = arr[1];
20+
edges[s].remove(Integer.valueOf(e));
21+
edges[e].remove(Integer.valueOf(s));
22+
int cnt1 = bfs(s);
23+
int cnt2 = bfs(e);
24+
res = Math.min(res, Math.abs(cnt1-cnt2));
25+
edges[s].add(e);
26+
edges[e].add(s);
27+
28+
}
29+
30+
return res;
31+
}
32+
static int bfs(int s) {
33+
int cnt = 1;
34+
Queue<Integer> q = new ArrayDeque<>();
35+
boolean[] visited = new boolean[n+1];
36+
q.offer(s);
37+
visited[s] = true;
38+
while(!q.isEmpty()) {
39+
cnt++;
40+
int cur = q.poll();
41+
for(Integer i : edges[cur]) {
42+
if(visited[i]) continue;
43+
q.offer(i);
44+
visited[i] = true;
45+
}
46+
}
47+
return cnt;
48+
}
49+
}

0 commit comments

Comments
 (0)