Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<int>parent;
- struct edge{
- int from, to, weight;
- edge(int u,int v, int w): from(u),to(v),weight(w){}
- bool operator < (const edge& p) const {
- return weight < p.weight;
- }
- };
- vector<edge>graph;
- int findParent(int node)
- {
- if(parent[node] == node)
- return node;
- return parent[node] = findParent(parent[node]);
- // return parent[node]==node ? node : parent[node] = findParent(parent[node]);
- }
- int mst(int nodeNumber)
- {
- sort(graph.begin(),graph.end());
- for(int i = 1 ;i <= nodeNumber; i++)
- parent[i] = i;
- int cost = 0;
- for(int i = 0 ; i < graph.size(); i++)
- {
- int from = findParent(graph[i].from);
- int to = findParent(graph[i].to);
- if(from!=to)
- {
- parent[to] = from;
- cost += graph[i].weight;
- }
- }
- return cost;
- }
- int main()
- {
- int nodeCount, edgeCount;
- cin >> nodeCount >> edgeCount;
- parent.resize(nodeCount+1);
- for(int i = 1 ; i <= edgeCount; i++)
- {
- int u,v,w;
- cin >> u >> v >> w;
- graph.push_back(edge(u,v,w));
- }
- cout<<mst(nodeCount)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment