Kruskal (MST): Really Special Subtree

  • + 0 comments

    include

    using namespace std;

    string ltrim(const string &); string rtrim(const string &); vector split(const string &);

    /* * Complete the 'kruskals' function below. * * The function is expected to return an INTEGER. * The function accepts WEIGHTED_INTEGER_GRAPH g as parameter. */

    int findParent(vector& parent, int x) { if (parent[x] == x) return x; return parent[x] = findParent(parent, parent[x]); }

    void unionSet(vector& parent, vector& rank, int x, int y) { int rootX = findParent(parent, x); int rootY = findParent(parent, y); if (rootX != rootY) { if (rank[rootX] < rank[rootY]) { parent[rootX] = rootY; } else if (rank[rootX] > rank[rootY]) { parent[rootY] = rootX; } else { parent[rootY] = rootX; rank[rootX]++; } } }

    int kruskals(int g_nodes, vector g_from, vector g_to, vector g_weight) { int numEdges = g_from.size(); vector>> edges(numEdges); for (int i = 0; i < numEdges; ++i) { edges[i] = {g_weight[i], {g_from[i], g_to[i]}}; } sort(edges.begin(), edges.end());

    vector<int> parent(g_nodes + 1);
    vector<int> rank(g_nodes + 1, 0);
    for (int i = 0; i <= g_nodes; ++i) {
        parent[i] = i;
    }
    
    int minWeight = 0;
    for (auto& edge : edges) {
        int w = edge.first;
        int u = edge.second.first;
        int v = edge.second.second;
        if (findParent(parent, u) != findParent(parent, v)) {
            unionSet(parent, rank, u, v);
            minWeight += w;
        }
    }
    return minWeight;
    

    }

    int main() { ofstream fout(getenv("OUTPUT_PATH"));

    string g_nodes_edges_temp;
    getline(cin, g_nodes_edges_temp);
    
    vector<string> g_nodes_edges = split(rtrim(g_nodes_edges_temp));
    
    int g_nodes = stoi(g_nodes_edges[0]);
    int g_edges = stoi(g_nodes_edges[1]);
    
    vector<int> g_from(g_edges);
    vector<int> g_to(g_edges);
    vector<int> g_weight(g_edges);
    
    for (int i = 0; i < g_edges; i++) {
        string g_from_to_weight_temp;
        getline(cin, g_from_to_weight_temp);
    
        vector<string> g_from_to_weight = split(rtrim(g_from_to_weight_temp));
    
        int g_from_temp = stoi(g_from_to_weight[0]);
        int g_to_temp = stoi(g_from_to_weight[1]);
        int g_weight_temp = stoi(g_from_to_weight[2]);
    
        g_from[i] = g_from_temp;
        g_to[i] = g_to_temp;
        g_weight[i] = g_weight_temp;
    }
    
    int result = kruskals(g_nodes, g_from, g_to, g_weight);
    
    fout << result << "\n";
    
    fout.close();
    
    return 0;
    

    }

    string ltrim(const string &str) { string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );
    
    return s;
    

    }

    string rtrim(const string &str) { string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );
    
    return s;
    

    }

    vector split(const string &str) { vector tokens;

    string::size_type start = 0;
    string::size_type end = 0;
    
    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));
    
        start = end + 1;
    }
    
    tokens.push_back(str.substr(start));
    
    return tokens;
    

    }