kruskal's algorithm c++ hackerearth
C++
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
const int MAX = 1e4 + 5;
int id[MAX], nodes, edges;
pair <long long, pair<int, int> > p[MAX];
void initialize()
{
for(int i = 0;i < MAX;++i)
id[i] = i;
}
int root(int x)
{
while(id[x] != x)
{
id[x] = id[id[x]];
x = id[x];
}
return x;
}
void union1(int x, int y)
{
int p = root(x);
int q = root(y);
id[p] = id[q];
}
long long kruskal(pair<long long, pair<int, int> > p[])
{
int x, y;
long long cost, minimumCost = 0;
for(int i = 0;i < edges;++i)
{
// Selecting edges one by one in increasing order from the beginning
x = p[i].second.first;
y = p[i].second.second;
cost = p[i].first;
// Check if the selected edge is creating a cycle or not
if(root(x) != root(y))
{
minimumCost += cost;
union1(x, y);
}
}
return minimumCost;
}
int main()
{
int x, y;
long long weight, cost, minimumCost;
initialize();
cin >> nodes >> edges;
for(int i = 0;i < edges;++i)
{
cin >> x >> y >> weight;
p[i] = make_pair(weight, make_pair(x, y));
}
// Sort the edges in the ascending order
sort(p, p + edges);
minimumCost = kruskal(p);
cout << minimumCost << endl;
return 0;
}
Also in C++:
- Title
- map.erase in c++
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- c++ write new file
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- c++ write string
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- for c++
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- initialize map c++
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- c++ give options string
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- basic data types in c++ hackerrank solution
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- array sort c++
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- sub string of vector c++
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- how to print to the serial monitor arduino
- Category
- C++
- Title
- how to convert string to int c++
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- ue4 c++ array
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- __builtin_ctz
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- Shortest Distance in a Maze
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- what is difference between single inverted and double inverted in programming languages
- Category
- C++
- Title
- how to use assrt c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++