-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnected-Island-Groups.cpp
68 lines (66 loc) · 1.77 KB
/
Connected-Island-Groups.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
/*
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 0
to V-1) and E connections or edges between islands. Can you count the number of connected groups of islands.
*/
#include <iostream>
#include <queue>
using namespace std;
void visitFiller(bool **edges, long long v, long long sv, bool *visited)
{
queue<long long> pendingVertices;
pendingVertices.push(sv);
visited[sv] = true;
while (!pendingVertices.empty())
{
for (long long i = 0; i < v; i++)
{
if (i == pendingVertices.front())
continue;
if (visited[i])
continue;
if (edges[pendingVertices.front()][i])
{
pendingVertices.push(i);
visited[i] = true;
}
}
pendingVertices.pop();
}
}
int main() {
long long v, e;
cin >> v >> e;
bool **edges = new bool *[v];
for (long long i = 0; i < v; i++)
{
edges[i] = new bool[v];
for (long long j = 0; j < v; j++)
{
edges[i][j] = false;
}
}
for (long long i = 0; i < e; i++)
{
long long start, end;
cin >> start >> end;
edges[start][end] = true;
edges[end][start] = true;
}
bool *visited = new bool[v];
for (long long i = 0; i < v; i++)
{
visited[i] = false;
}
int count = 0;
for (long long i = 0; i < v; i++)
{
if (!visited[i])
{
count++;
visitFiller(edges, v, i, visited);
}
}
cout << count;
return 0;
}