프로그래머스 탐욕법 섬 연결하기 c++ 풀이

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

vector<bool> visited(101, false);

bool compare(const vector<int> &a, const vector<int> &b)
{
    return a[2] < b[2];
}

bool isCycle(vector<int> &cost)
{
    if (visited[cost[0]] && visited[cost[1]])
    {
        return true;
    }
    return false;
}

int solution(int n, vector<vector<int> > costs)
{
    int answer = 0;

    sort(costs.begin(), costs.end(), compare);

    for (int i = 0, count = 0; i < costs.size() && count != n - 1; i++)
    {
        if (!isCycle(costs[i]))
        {
            visited[costs[i][0]] = true;
            visited[costs[i][1]] = true;
            answer += costs[i][2];
            count++;
        }
    }

    return answer;
}