문제 링크
난이도 : Lv. 2
문제 내용
문제 설명
n개의 송전탑이 전선을 통해 하나의 트리 형태로 연결되어 있습니다. 당신은 이 전선들 중 하나를 끊어서 현재의 전력망 네트워크를 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
- 다음 그림은 주어진 입력을 해결하는 방법 중 하나를 나타낸 것입니다.

- 4번과 7번을 연결하는 전선을 끊으면 두 전력망은 각 6개와 3개의 송전탑을 가지며, 이보다 더 비슷한 개수로 전력망을 나눌 수 없습니다.
- 또 다른 방법으로는 3번과 4번을 연결하는 전선을 끊어도 최선의 정답을 도출할 수 있습니다.
입출력 예 #2
- 다음 그림은 주어진 입력을 해결하는 방법을 나타낸 것입니다.

- 2번과 3번을 연결하는 전선을 끊으면 두 전력망이 모두 2개의 송전탑을 가지게 되며, 이 방법이 최선입니다.
입출력 예 #3
- 다음 그림은 주어진 입력을 해결하는 방법을 나타낸 것입니다.

- 3번과 7번을 연결하는 전선을 끊으면 두 전력망이 각각 4개와 3개의 송전탑을 가지게 되며, 이 방법이 최선입니다.
문제 분석
하나씩 끊어보고 확인하기
작성한 코드
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
vector<vector<int>> graph;
vector<bool> visited;
stack<int> nextNodes;
void dfs(int current)
{
if(visited[current]) return;
visited[current] = true;
for(const auto &w : graph[current])
{
if(!visited[w])
{
nextNodes.push(w);
}
}
while(!nextNodes.empty())
{
int nextNode = nextNodes.top();
nextNodes.pop();
dfs(nextNode);
}
}
int solution(int n, vector<vector<int>> wires) {
graph.assign(n+1, {});
for(const auto &w : wires)
{
graph[w[0]].push_back(w[1]);
graph[w[1]].push_back(w[0]);
}
int minDiff = 99999;
for(const auto &w : wires)
{
while(!nextNodes.empty()) nextNodes.pop();
visited.assign(n+1, false);
graph[w[0]].erase(find(graph[w[0]].begin(), graph[w[0]].end(), w[1]));
graph[w[1]].erase(find(graph[w[1]].begin(), graph[w[1]].end(), w[0]));
nextNodes.push(1);
dfs(1);
graph[w[0]].push_back(w[1]);
graph[w[1]].push_back(w[0]);
int first = 0;
int second = 0;
for(int i = 1 ; i <= n; i++)
{
if(visited[i]) first++;
}
second = n - first;
if(abs(first-second) < minDiff)
{
minDiff = abs(first-second);
}
}
return minDiff;
}우수 코드 분석
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std;
int dfs(int node, int parent, const vector<vector<int>> &graph)
{
int cnt = 1;
for (int child : graph[node])
{
if (child != parent)
{
cnt += dfs(child, node, graph);
}
}
return cnt;
}
int solution(int n, vector<vector<int>> wires)
{
vector<vector<int>> graph(n+1);
for(auto &wire : wires)
{
int a = wire[0];
int b = wire[1];
graph[a].push_back(b);
graph[b].push_back(a);
}
int min_diff = INT_MAX;
for (auto &wire : wires)
{
int a = wire[0];
int b = wire[1];
graph[a].erase(remove(graph[a].begin(), graph[a].end(), b), graph[a].end());
graph[b].erase(remove(graph[b].begin(), graph[b].end(), a), graph[b].end());
int cnt_a = dfs(a, b, graph);
int cnt_b = n - cnt_a;
min_diff = min(min_diff, abs(cnt_a - cnt_b));
graph[a].push_back(b);
graph[b].push_back(a);
}
return min_diff;
}