Hackerearth So NP Graph Representation problem Solution

Solution in C++:

///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 10;
vector<int>g[mx];
bool v[mx];
void dfs(int s)
{
v[s]=1;
for(int i=0; i<g[s].size(); i++)
{
int x=g[s][i];
if(v[x]==0)
dfs(x);
}
}
int main()
{
int n,m,k;
cin >> n >>m>>k;
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int c=0;
for(int i=1; i<=n; i++)
{
if(!v[i])
{
dfs(i);
c++;
}

}
cout<<(c>k ? -1 :m-(n-k))<<endl;
}

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.