// C++ program to find efficient
// solution for the network
#include <cstring> // For memset
#include <iostream>
#include <vector>
using namespace std;
// number of houses and number
// of pipes
int number_of_houses, number_of_pipes;

// Array rd stores the
// ending vertex of pipe
int ending_vertex_of_pipes[1100];

// Array wd stores the value
// of diameters between two pipes
int diameter_between_two_pipes[1100];

// Array cd stores the
// starting end of pipe
int starting_vertex_of_pipes[1100];

// Vector a, b, c are used
// to store the final output
vector<int> a;
vector<int> b;
vector<int> c;

int ans;

int dfs(int w)
{
    if (starting_vertex_of_pipes[w] == 0)
        return w;
    if (diameter_between_two_pipes[w] < ans)
        ans = diameter_between_two_pipes[w];
    return dfs(starting_vertex_of_pipes[w]);
}

// Function performing calculations.
void solve(int arr[][3])
{
    for (int i = 0; i < number_of_pipes; ++i) {

        int house_1 = arr[i][0], house_2 = arr[i][1],
            pipe_diameter = arr[i][2];

        starting_vertex_of_pipes[house_1] = house_2;
        diameter_between_two_pipes[house_1] = pipe_diameter;
        ending_vertex_of_pipes[house_2] = house_1;
    }

    a.clear();
    b.clear();
    c.clear();

    for (int j = 1; j <= number_of_houses; ++j)

        /*If a pipe has no ending vertex
        but has starting vertex i.e is
        an outgoing pipe then we need
        to start DFS with this vertex.*/
        if (ending_vertex_of_pipes[j] == 0
            && starting_vertex_of_pipes[j]) {
            ans = 1000000000;
            int w = dfs(j);

            // We put the details of component
            // in final output array
            a.push_back(j);
            b.push_back(w);
            c.push_back(ans);
        }

    cout << a.size() << endl;
    for (int j = 0; j < a.size(); ++j)
        cout << a[j] << " " << b[j] << " " << c[j] << endl;
}

// driver function
int main()
{
    number_of_houses = 9, number_of_pipes = 6;

    memset(ending_vertex_of_pipes, 0,
           sizeof(ending_vertex_of_pipes));
    memset(starting_vertex_of_pipes, 0,
           sizeof(starting_vertex_of_pipes));
    memset(diameter_between_two_pipes, 0,
           sizeof(diameter_between_two_pipes));

    int arr[][3]
        = { { 7, 4, 98 }, { 5, 9, 72 }, { 4, 6, 10 },
            { 2, 8, 22 }, { 9, 7, 17 }, { 3, 1, 66 } };

    solve(arr);
    return 0;
}

This is the code format in which I want my code to be in without changing variable name and also use  vector instead of array 
#include <bits/stdc++.h>
using namespace std;

int ans;
void printSolution(vector<int> &st, vector<int> &en, vector<int> &dm)
{
    cout << st.size() << endl;
    for (int j = 0; j < st.size(); ++j)
        cout << st[j] << " " << en[j]
             << " " << dm[j] << endl;
}
int dfs(int w, vector<int> &diameter, vector<int> &start)
{
    if (start[w] == 0)
        return w;
    if (diameter[w] < ans)
        ans = diameter[w];
    return dfs(start[w], diameter, start);
}

// function for water connection problem
void water_connection(int arr[][3], int n, int pipes)
{
    // to store the starting vertex of the pipe
    vector<int> start(1100, 0);
    // to store the ending vertex of the pipe
    vector<int> ed(1100, 0);
    // to store the diameter of pipe
    vector<int> diameter(1100);
    int i = 0;

    for (i = 0; i < pipes; i++)
    {
        start[arr[i][0]] = arr[i][1];
        diameter[arr[i][0]] = arr[i][2];
        ed[arr[i][1]] = arr[i][0];
    }
    // st---> to store the starting vertex
    // en--> to store the ending vertex
    //  dm-->to store the minimum diameter of the pipe
    vector<int> st, en, dm;
    for (int j = 1; j <= n; ++j)

        // If there is a pipe that has no ending vertex but has
        // starting vertex then we will start DFS with this vertex
        if (ed[j] == 0)
        {
            if (start[j])
            {
                ans = INT_MAX;
                int w = dfs(j, diameter, start);

                // we will store the details of components in these final arrays
                st.push_back(j);
                en.push_back(w);
                dm.push_back(ans);
            }
        }
    printSolution(st, en, dm);
}

// driver function
int main()
{
    // to store the no of houses and pipes
    int n = 9, pipes = 6;
    int arr[][3] = {{2, 3, 45},
                    {4, 5, 10},
                    {5, 8, 22},
                    {1, 2, 33},
                    {7, 6, 17},
                    {3, 7, 66}};

    water_connection(arr, n, pipes);
    return 0;
}
do it from this