Skip to content

Commit 76da875

Browse files
authored
Adding CODECHEF Problem
Counting the number of components is simple dfs implementation. A simple knowledge of DFS is enough for this problem. We just need to run dfs starting from any node. In one dfs all the nodes of the same component will be visited. Thus this is one component. The count of dfs run is simply the number of components in the graph The sample above is FIRESC taken from CODECHEF. URL: https://www.codechef.com/problems/FIRESC
1 parent 37e746f commit 76da875

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* counting the number of components is simple dfs implementation.
2+
A simple knowledge of DFS is enough for this problem.
3+
We just need to run dfs starting from any node.
4+
In one dfs all the nodes of the same component will be visited.
5+
Thus this is one component.
6+
The count of dfs run is simply the number of components in the graph
7+
*/
8+
9+
10+
#include<iostream>
11+
using namespace std;
12+
#include<queue>
13+
#include<map>
14+
#include<math.h>
15+
#define lli long long int
16+
#include<vector>
17+
#define vi vector<int>
18+
#define mod 1000000007
19+
#define pb push_back
20+
#define fo(i,n) for(int i=1;i<=n;i++)
21+
22+
#include <bits/stdc++.h>
23+
24+
vi adj[100003];
25+
int vis[100003],member;
26+
27+
void dfs(int node){
28+
vis[node]=1;
29+
member++;
30+
for(int child: adj[node]){
31+
if(!vis[child]){
32+
dfs(child);
33+
}
34+
}
35+
}
36+
37+
int main(){
38+
int t,n,m,a,b;
39+
cin>>t;
40+
41+
while(t--){
42+
lli ans=1,cc=0;
43+
44+
cin>>n>>m;
45+
46+
fo(i,n) vis[i]=0, adj[i].clear();
47+
48+
fo(i,m) cin>>a>>b, adj[a].pb(b), adj[b].pb(a);
49+
fo(i,n)
50+
if(!vis[i]){
51+
member=0;
52+
cc++; dfs(i);
53+
ans = (ans * member)%mod;
54+
}
55+
cout<<cc<<" "<<ans<<endl;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)