-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnected_Computer_networks.cpp
73 lines (65 loc) · 1.7 KB
/
Connected_Computer_networks.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi]
// represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
// You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.
// Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.
// input = number of nodes n
// number of edges m
// m number of edges
// output = integer
// sample cases:
// input =
// 4
// 3
// 0 1
// 0 2
// 1 2
// output = 1
// case 2:
// input = 6
// 5
// 0 1
// 0 2
// 0 3
// 1 2
// 1 3
// output = 2
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,op;
cin>>n;
cin>>op;
unordered_map<int,vector<int>> m;
for(int i=0;i<op;i++){
int x,y;
cin>>x>>y;
m[x].push_back(y);
m[y].push_back(x);
}
queue<int> q;
vector<int> visited(n,false);
int c=0;
for(int i=0;i<n;i++){
if(visited[i]==false){
visited[i]=true;
q.push(i);
c++;
while(!q.empty()){
int front=q.front();
q.pop();
for(auto x:m[front]){
if(visited[x]==false){
visited[x]=true;
q.push(x);
}
}
}
}
}
if(n-1<=op){
cout<<c-1;
}
else{
cout<<-1;
}
}