-
Notifications
You must be signed in to change notification settings - Fork 10
/
HackerEarth-So NP.cpp
52 lines (48 loc) · 900 Bytes
/
HackerEarth-So NP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
void dfs(int u,bool visited[],vector<int>V[])
{
visited[u]=true;
vector<int>::iterator it;
for(it=V[u].begin();it!=V[u].end();it++)
{
if(!visited[*it])
{
dfs((*it),visited,V);
}
}
}
int main()
{
int n,m,k,u,v;
cin>>n>>m>>k;
vector<int>V[n+1];
int sz=0;
for(int i=0;i<m;i++)
{
cin>>u>>v;
V[u].push_back(v);
V[v].push_back(u);
}
bool * visited=new bool [n+1];
memset(visited,false,sizeof(visited));
int num_of_connected=0;
for(int i=1;i<=n;i++)
{
if(visited[i]==false)
{
dfs(i,visited,V);
//cout<<i<<endl;
num_of_connected++;
}
}
if(num_of_connected>k)
{
cout<<"-1"<<endl;
}
else
{
cout<<(m-n+k)<<endl;
}
return 0;
}