Programmers, ์„ฌ ์—ฐ๊ฒฐํ•˜๊ธฐ

2021. 12. 2. 23:06ใ†ETC/Algorithm

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋Œ€ํ‘œ์ ์ธ MST์œผ๋กœ Kruskal's algorithm์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

 

๋ฌธ์ œ ์„ค๋ช…

n๊ฐœ์˜ ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๋ฅผ ๊ฑด์„คํ•˜๋Š” ๋น„์šฉ(costs)์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ์˜ ๋น„์šฉ์œผ๋กœ ๋ชจ๋“  ์„ฌ์ด ์„œ๋กœ ํ†ตํ–‰ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ์„ return ํ•˜๋„๋ก solution์„ ์™„์„ฑํ•˜์„ธ์š”.

๋‹ค๋ฆฌ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฑด๋„ˆ๋”๋ผ๋„, ๋„๋‹ฌํ•  ์ˆ˜๋งŒ ์žˆ์œผ๋ฉด ํ†ตํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ด…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด A ์„ฌ๊ณผ B ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๊ณ , B ์„ฌ๊ณผ C ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด A ์„ฌ๊ณผ C ์„ฌ์€ ์„œ๋กœ ํ†ตํ–‰ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • ์„ฌ์˜ ๊ฐœ์ˆ˜ n์€ 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • costs์˜ ๊ธธ์ด๋Š” ((n-1) * n) / 2์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ž„์˜์˜ i์— ๋Œ€ํ•ด, costs[i][0] ์™€ costs[i] [1]์—๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋Š” ๋‘ ์„ฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋“ค์–ด์žˆ๊ณ , costs[i] [2]์—๋Š” ์ด ๋‘ ์„ฌ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋‹ค๋ฆฌ๋ฅผ ๊ฑด์„คํ•  ๋•Œ ๋“œ๋Š” ๋น„์šฉ์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์—ฐ๊ฒฐ์€ ๋‘ ๋ฒˆ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„ ๊ฐ™์€ ์—ฐ๊ฒฐ๋กœ ๋ด…๋‹ˆ๋‹ค. ์ฆ‰ 0๊ณผ 1 ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๊ณผ 0์˜ ๋น„์šฉ์ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์„ฌ ์‚ฌ์ด์˜ ๋‹ค๋ฆฌ ๊ฑด์„ค ๋น„์šฉ์ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋‘ ์„ฌ ์‚ฌ์ด์˜ ๊ฑด์„ค์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๋ด…๋‹ˆ๋‹ค.
  • ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ์„ฌ์€ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ


n costs return
4 [[0,1,1],[0,2,2],[1,2,5],[1,3,1],[2,3,8]] 4

 

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

costs๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ์ด๋•Œ ์ดˆ๋ก์ƒ‰ ๊ฒฝ๋กœ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ ์€ ๋น„์šฉ์œผ๋กœ ๋ชจ๋‘๋ฅผ ํ†ตํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ

 

C++

#include <algorithm>
#include <vector>

using namespace std;

struct Edge {
    int node[2];
    int distance;
    
    Edge(int a, int b, int distance) {
        this->node[0] = a;
        this->node[1] = b;
        this->distance = distance;
    }
    
    bool operator <(const Edge &edge) const {
        return this->distance < edge.distance;
    }
};

int getRoot(int root[], int n) {
    if (root[n] == n) return n;
    return root[n] = getRoot(root, root[n]);
}

bool isCycle(int root[], int a, int b) {
    a = getRoot(root, a);
    b = getRoot(root, b);
    return a == b;
}

void unionRoot(int root[], int a, int b) {
    a = getRoot(root, a);
    b = getRoot(root, b);
    if (a < b) root[b] = a;
    else root[a] = b;
}

int solution(int n, vector<vector<int>> costs) {
    int answer = 0;
    int root[n];
    
    vector<Edge> v;
    for (vector<int> c: costs) {
        v.push_back(Edge(c[0], c[1], c[2]));
    }
    
    sort(v.begin(), v.end());
    
    for (int i=0; i<n; i++) root[i] = i;
    
    for (int i=0; i< v.size(); i++) {
        if(!isCycle(root, v[i].node[0], v[i].node[1])) {
            answer += v[i].distance;
            unionRoot(root, v[i].node[0], v[i].node[1]);
        }
    }
    
    return answer;
}