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 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

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

Powered by Blogger.