Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ed86244

Browse files
Added more stuff
1 parent 8e2d004 commit ed86244

File tree

3 files changed

+124
-7
lines changed

3 files changed

+124
-7
lines changed

‎Graphs1/Islandsc.cpp‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
/*
8+
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 N islands (numbered from 1 to N) and two lists of size M (u and v) denoting island u[i] is connected to island v[i] and vice versa . Can you count the number of connected groups of islands.
9+
Constraints :
10+
1<=N<=100
11+
1<=M<=(N*(N-1))/2
12+
1<=u[i],v[i]<=N
13+
Input Format :
14+
Line 1 : Two integers N and M
15+
Line 2 : List u of size of M
16+
Line 3 : List v of size of M
17+
Output Return Format :
18+
The count the number of connected groups of islands
19+
Sample Input :
20+
2 1
21+
1
22+
2
23+
Sample Output :
24+
1
25+
*/
26+
27+
#include <bits/stdc++.h>
28+
29+
using namespace std;
30+
31+
void DFS(int** arr, int* visited, int n, int sv){
32+
33+
visited[sv] = 1;
34+
35+
for (int i = 0; i < n; ++i)
36+
{
37+
if (i==sv)
38+
{
39+
continue;
40+
}
41+
if (arr[sv][i] == 1 && visited[i] == 0)
42+
{
43+
DFS(arr, visited, n, i);
44+
}
45+
}
46+
47+
return;
48+
}
49+
50+
int solve(int n,int m,vector<int>u,vector<int>v)
51+
{
52+
int** arr = new int*[n];
53+
for (int i = 0; i < n; ++i)
54+
{
55+
arr[i] = new int[n];
56+
for (int j = 0; j < n; ++j)
57+
{
58+
arr[i][j] = 0;
59+
}
60+
}
61+
62+
63+
64+
for (int i = 0; i < m; ++i)
65+
{
66+
arr[u[i]-1][v[i]-1] = 1;
67+
arr[v[i]-1][u[i]-1] = 1;
68+
}
69+
70+
// for (int i = 0; i < n; ++i)
71+
// {
72+
// for (int j = 0; j < n; ++j)
73+
// {
74+
// cout<<i<<" "<<j<<" "<<arr[i][j]<<endl;
75+
// }
76+
// }
77+
// cout << '\n';
78+
79+
int* visited = new int[n];
80+
81+
for (int i = 0; i < n; ++i)
82+
{
83+
visited[i] = 0;
84+
}
85+
86+
int count = 0;
87+
for (int i = 0; i < n; ++i)
88+
{
89+
if (visited[i]==0)
90+
{
91+
count++;
92+
DFS(arr, visited, n, i);
93+
}
94+
}
95+
96+
return count;
97+
}
98+
99+
int main( int argc , char ** argv )
100+
{
101+
ios_base::sync_with_stdio(false) ;
102+
cin.tie(NULL) ;
103+
104+
int n,m;
105+
vector<int>u,v;
106+
cin>>n>>m;
107+
for(int i=0;i<m;i++)
108+
{
109+
int x;
110+
cin>>x;
111+
u.push_back(x);
112+
}
113+
for(int i=0;i<m;i++)
114+
{
115+
int x;
116+
cin>>x;
117+
v.push_back(x);
118+
}
119+
cout<<solve(n,m,u,v)<<endl;
120+
121+
}

‎Graphs1/Test.txt‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
5 5
2-
0 1
3-
0 2
4-
1 3
5-
1 4
6-
3 4
7-
0 2
1+
2 1
2+
1
3+
2

‎Graphs1/a.out‎

15.3 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /