"find the graph is minimal spanig tree or not" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "find the graph is minimal spanig tree or not" answered properly. Developers are finding an appropriate answer about find the graph is minimal spanig tree or not related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like find the graph is minimal spanig tree or not. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on find the graph is minimal spanig tree or not. 

find the graph is minimal spanig tree or not

By Comfortable CassowaryComfortable Cassowary on May 10, 2020
#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;
}

Source: www.hackerearth.com

Add Comment

1

All those coders who are working on the C++ based application and are stuck on find the graph is minimal spanig tree or not can get a collection of related answers to their query. Programmers need to enter their query on find the graph is minimal spanig tree or not related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about find the graph is minimal spanig tree or not for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of find the graph is minimal spanig tree or not while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "find the graph is minimal spanig tree or not". Visit this developer's friendly online web community, CodeProZone, and get your queries like find the graph is minimal spanig tree or not resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "find the graph is minimal spanig tree or not"

View All C++ queries

C++ queries related to "find the graph is minimal spanig tree or not"

find the graph is minimal spanig tree or not Write a program in C++ to find post-order predecessor of a node in a Binary Tree graph using djacency matrix c++ bfs traversal of graph in c graph c++ Dijkstra's Weighted Graph Shortest Path in c++ graph using queue c++ adjacency matrix of a directed graph delete and search edge in adjacency matrix of a graph Graph Adjacent Node in c++ weighted graph c++ adjacency list representation of graph gfg bottom view of tree gfg right view of tree binary search tree in cpp using class binary index tree c++ bst to insert tree binary indexed tree gfg left view of tree tree in c++ stl gfg top view of tree dfenwick tree code c++ binary tree search deletion in a binary search tree binary tree deletion avl tree implementation c++ top view of binary tree c++ heap sort heapify and max heap in binary tree diameter of tree using dfs binary search tree sorted order searching display insert in a binary serach tree centroid of a tree vertical traversal of binary tree Print Nodes in Top View of Binary Tree could not find the task c c++ active file find last element of an array c++ how to find hcf in c++ find all occurrences of a substring in a string c++ c++ find prime numbers find function in c++ how to find absolute value in c++ map in c++ find whether key exists find index of element in vector c++ find vector in c++ code to find the last digit of a number find length of array c++ find in set of pairs using first value cpp find the biggest number from 3 numbers c++ How to find the suarray with maximum sum using divide and conquer vector.find() find min and max in array c++ find character in string c++ find minimum value in vector c++ c++ map find c++ find element in vector Write a program to find the sum of all sub-arrays of a given integer array. how to find 2d vector length cpp c++ find element in set find last occurrence of character in string c++ find last digit of number find in vector std string find character c++ c++ find number of divisors how to find last character of string in c++ how to find length of character array in c++ string .find in c++ how to find length of string in c++ how to find the mode of a vector c++ find in vector c++ cpp program to find average of n numbers c++ std::find with lambda find all the palindrome substring in a given string c++ function to find length of array c++ find string in string find substring in string c++ c++ program to find gcd of 3 numbers find vector size in c++ how to find the size of a character array in c++ c++ two array find same values how to find data size in c++ Find N Unique Integers Sum Up to Zero find number of 1s in a binary cv::mat image how to find a integer is how many times repeated in C++ without for loop how to find sum of values on path in atree how to find the left most bit 1 in binary of any number Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2 how to find the max b=etween 3number in cpp how to find quotient and remainder in c++ Find Missing And Repeating s.find c++reference c++ find object in vector by attribute how to find all permutations of n distinct integers in c++ c++ find with predicat find mod in cpp find a number in vector c++ find pair in unsorted array which gives sum x how to find the length of an string in c++ find in string c++ find vector push_back in c++ find max value in array c++ find in c++ how to find maximum value in c++ find nth word in sentence c++ find string cpp map find c++ does not name a type cout does not name a type how to check array is sorted or not in c++ how to make sure the user inputs a int and not anything else c++ void value not ignored as it ought to be remove () not working c++ c++ default array value not null dlopen failed: library "libomp.so" not found c++ how to loop through a vector but not the last element ‘setprecision’ was not declared in this scope how to ensure the user inouts a int and not anything else c++ error: ‘memset’ was not declared in this scope in cpp fork was not declared in this scope 0009:err:mscoree:CLRRuntimeInfo_GetRuntimeHost Wine Mono is not installed if not defined c++ sass set variable if not defined cin does not wait for input if not c++ count was not declared in this scope c++ codeblocks is variable sized array are not allowed in c++? gtest assert not equal c++ fstream create if not exists error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope error: 'std::high_resolution_clock' has not been declared Write a function called clean that takes a C++ string as input and removes any characters in the string that are not letters except for space blanks. c++ scanf always expects double and not float c++ vector push if not exist c++ string not printing void does not a name a type in cpp c++ print number not in scientific notation Check whether K-th bit is set or not c++ C++ pointer to incomplete class type is not allowed unreal check if equal to \ char or not c++ is not a nonstatic data member or base class of class substitution failure is not an error Not c++ c++ public inheritance not getting protected Visual Studio Code: code not running for C++11 c++ do you not inherit constructor c++ operator overloading not equal system was not declared in this scope 51 13 C:\Users\usuario\Documents\Destajo 1.cpp [Error] 'gotoxy' was not declared in this scope rc.local not running centos 6 ceil in cpp not rounding off properly

Browse Other Code Languages

CodeProZone