We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Kruskal (MST): Really Special Subtree
Kruskal (MST): Really Special Subtree
Sort by
recency
|
187 Discussions
|
Please Login in order to post a comment
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star:) )
My python solution
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());
}
int main() { ofstream fout(getenv("OUTPUT_PATH"));
}
string ltrim(const string &str) { string s(str);
}
string rtrim(const string &str) { string s(str);
}
vector split(const string &str) { vector tokens;
}
Golang solution
C++ solution: