Codechef Connected Components Solution




Problem Link :  https://www.codechef.com/problems/CC

 Solution in C++:  

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
vector <int> vec[100009];
bool visited[100009];
void dfs(int source)
{
    visited[source] = 1;
    for (int i = 0; i < vec[source].size(); i++)
    {
        int next = vec[source][i];
        if (visited[next] == 0)
            dfs(next);
    }
}
int main()
{
    int nodes, edges,t;
    cin>>t;
    while(t--)
    {
        cin >> nodes >> edges;
        for(int i=0; i<nodes; i++)
        {
            vec[i].clear();
            visited[i]=false;
        }
        for (int i = 0; i < edges; i++)
        {
            int u, v;
            cin >> u >> v;
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        long int cnt=0;
        for (int i = 0; i < nodes; i++)
        {
            if(!visited[i])
            {
                cnt++;
                dfs(i);
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}









No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.