forked from recongamer/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinding No of Connected Components in Graph .cpp
69 lines (59 loc) · 1.75 KB
/
Finding No of Connected Components in Graph .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
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define lli long long int
#define ll long long
#define pb push_back
#define pob pop_back
#define pii pair<int,int>
#define vi vector<int>
#define vll vector <ll>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define mod 1000000007
#define inf1 1e9
#define inf2 1e18
#define N 200009
#define loop(i,a,b) for (int i=a;i<b;i++)
#define PI 3.14159265358979323846
#define np next_permutation
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(tc) int tc; cin>>tc; while(tc--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void methoding()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// ******************************************************************************
vi adj[2001] ; int vis[2001];
void dfs ( int n ) {
vis[n] = 1;
for ( int i = 0; i < adj[n].size(); i++) {
int c = adj[n][i];
if (vis[c] == 0)
dfs(c);
}
}
int32_t main() {
methoding();
w(tc) {
int n , m ; cin >> n >> m;
memset(adj , 0, sizeof(adj)); memset(vis , 0, sizeof(vis));
while (m--) {
int x , y; cin >> x >> y;
adj[x].pb(y) ; adj[y].pb(x);
}
int c = 0;
loop(i, 1, n + 1) {
if (vis[i] == 0) {dfs(i); c++;}
}
cout << "Connected Components :: " << c << endl;
}
return 0;
}