-
Notifications
You must be signed in to change notification settings - Fork 1
/
Islands.cpp
95 lines (77 loc) · 2.05 KB
/
Islands.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Islands
An island is a small piece of land surrounded by water . A group of islands is said to be connected if we can reach from any given island to
any other island in the same group . Given V islands (numbered from 1 to V) and E connections or edges between islands.
Can you count the number of connected groups of islands.
Input Format :
The first line of input contains two integers, that denote the value of V and E.
Each of the following E lines contains two integers, that denote that there exists an edge between vertex a and b.
Output Format :
Print the count the number of connected groups of islands
Constraints :
0 <= V <= 1000
0 <= E <= (V * (V-1)) / 2
0 <= a <= V - 1
0 <= b <= V - 1
Time Limit: 1 second
Sample Input 1:
5 8
0 1
0 4
1 2
2 0
2 4
3 0
3 2
4 3
Sample Output 1:
1
*/
#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#define line '\n'
using namespace std;
void BFS(unordered_map<int, vector<int>>& graph, vector<bool>& visited, int start) {
queue<int> q;
q.push(start);
visited[start] = true;
while(!q.empty()) {
int front = q.front();
q.pop();
for(int i = 0; i < graph[front].size(); i++) {
int current = graph[front][i];
if(!visited[current]) {
q.push(current);
visited[current] = true;
}
}
}
}
int countComponents(unordered_map<int, vector<int>>& graph, int v) {
vector<bool> visited(v, false);
int count = 0;
for(int i = 0; i < v; i++) {
if(!visited[i]) {
count++;
BFS(graph, visited, i);
}
}
return count;
}
int main() {
// Write your code here
int v, e;
cin >> v >> e;
unordered_map<int, vector<int>> graph;
for(int i = 0; i < e; i++) {
int first, second;
cin >> first >> second;
graph[first].push_back(second);
graph[second].push_back(first);
}
cout << countComponents(graph, v) << line;
}
// Time Complexity : O(V + E)
// Auxillary Space : O(V)