1

I have a undirected graph (may be disconnected graph). I need to find Number of unreachable nodes for a given node.

#include<bits/stdc++.h>
using namespace std;
vector<int> graph[1000];
bool visited[1000];
int count=0;

void dfs(int s)
{
  count=count+1;
  visited[s]=true;
  for(int i=0;i<graph[s].size();i++)
    if(visited[graph[s][i]]==false)
      dfs(i);
}

int main()
{
  int n,m;
  cin>>n>>m;
  int i;
  for(i=0;i<m;i++)
  {
    int x,y; 
    cin>>x>>y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  int k;
  cin>>k;
  dfs(k);

  cout<<n-count;
}

Initially the graph has n nodes. After DFS process, for the particular node k ,dfs(k) finds number of nodes connected with k. So the unreachable nodes can be calculated by n-count.

But this codes shows an error that says reference to 'count' is ambiguous. What is the problem? Did I make any mistake in DFS recursive method?

SaravananKS
  • 577
  • 4
  • 18
  • 1
    This is why you shouldn't pollute namespaces. `using namespace std;` is the culprit. Read [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Yashas Dec 14 '17 at 13:19

1 Answers1

2

In C++ library there is count function template - in algorithm header (which was included by #include <bits/stdc++.h> directive), you can resolve your problem by adding :: before count in dfs and main function.

  ::count= ::count+1;

and

  cout<<n-::count;
rafix07
  • 20,001
  • 3
  • 20
  • 33