[프로그래머스 / C++] 전력망을 둘로 나누기

Seulguo·2022년 7월 13일
0

Algorithm

목록 보기
56/185
post-thumbnail
post-custom-banner

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/86971


🐥 코드

#include <string>
#include <vector>

using namespace std;
vector<int> v[200];

int dfs(int to, int from, int count){
    for(int i = 0; i < v[from].size(); i++){
        if(v[from][i] != to){
            count = dfs(from, v[from][i], count+1);
        }
    }
    return count;
}

int solution(int n, vector<vector<int>> wires) {
    int answer = 100;
    
    for (auto w : wires){
        v[w[0]].push_back(w[1]);
        v[w[1]].push_back(w[0]);
    }
    
    for (auto w : wires){
        int sum = bfs(w[0], w[1], 1);
        int comp = n - (2 * sum);
        answer = min(answer, abs(comp));
    }
    
    return answer;
}
post-custom-banner

0개의 댓글