forked from yashasvi-goel/Basic-C-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edgeCountDfs.cpp
62 lines (52 loc) · 1.12 KB
/
edgeCountDfs.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
/*Author: Sahil Kalamkar
Date: 13/10/2019
Program to count the number of edges in a simple undirected graph using DFS.
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
//Declaring the adjacency list
vector<int> v[maxn];
//Initializing the visited array with false.
bool visited[maxn]={false};
//Variable which will store the final answer.
int edgecount = 0;
void dfs(int node)
{
//Marking the encountered node as visited.
visited[node] = true;
//Iterating over all it's neighbours.
for(int i:v[node])
{
//Incrementing the edgecount for all the neighbours.
edgecount++;
if(!visited[i])
{
dfs(i);
}
}
}
int main()
{
//Fast Input Output.
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//Taking input of n nodes and m edges.
int n,m;
cin>>n>>m;
int x,y;
while(m--)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
//Assuming 1 to be the root.
dfs(1);
cout<<"The number of edges in the graph is "<<'\n';
//In the dfs call each edge is being called twice due to which we divide the edgecount by 2.
cout<<edgecount/2<<'\n';
return 0;
}