Search code examples
c++for-loopgraphauto

Why is my for loop giving error : X does not name a type


Can anybody tell me that when I use for loop to get the number of nodes stored in level 'it', then why it doesn't work?

And please do tell me other ways I can access a vector with a range based for loop.

// A simple representation of graph using STL 
#include<iostream>
#include<vector>
using namespace std;

// A utility function to add an edge in an 
// undirected graph. 
void addEdge(vector<int> adj[], int u, int v) 
{ 
    adj[u].push_back(v); 
    adj[v].push_back(u); 
} 


void printNodes(vector<int> adj[], int n) 
{ int count=0;
        for (auto x : adj[n]){
            count++;
        } 
        cout<<count;

} 

// Driver code 
int main() 
{ 

int V,x,y;
cin>>V;

    vector<int> adj[V+1]; 

    for(int i=0;i<V-1;i++){
        cin>>x>>y;
    addEdge(adj, x, y); 
}
    int it;
    cin>>it;
    printNodes(adj, it); 
    return 0; 
} 


Solution

  • First of all, there are some things missing in your approach:

    1. You are adding edges in the graph and there is no input for it, i have assumed that V is for vertices only.
    2. Whenever you want to check for the number of nodes in a particular level you should always mention a starting point. Because different starting points may account to different outputs in a graph which is not rooted like a tree.
    3. If the number of vertices is V, then the vector should be vector adj[V+1], if you want your vertices to be 1-indexed.

    So here is the final code:

    #include<iostream>
    #include<vector>
    #include<queue>
    using namespace std;
    
    
    int nodes_at_level[10];
    // Taken from hackerearth.....
    
    
        int level[10]; //To determine the level of each node
        bool vis[10]; //Mark the node if visited 
    
        void bfs(int s,vector<int> adj[]) {
            queue <int> q;
            q.push(s);
            level[ s ] = 0 ;  //Setting the level of the source node as 0
            nodes_at_level[level[s]]++;
            vis[ s ] = true;
            while(!q.empty())
            {
                int p = q.front();
                q.pop();
                for(int i = 0;i < adj[ p ].size() ; i++)
                {
                    if(vis[ adj[ p ][ i ] ] == false)
                    {
                //Setting the level of each node with an increment in the level of parent node
                        level[ adj[ p ][ i ] ] = level[ p ]+1;
    
                        nodes_at_level[level[ adj[ p ][ i ] ]]++;
    
                         q.push(adj[ p ][ i ]);
                         vis[ adj[ p ][ i ] ] = true;
                    }
                }
            }
        }
    
    void addEdge(vector<int> adj[], int u, int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    //to print the number of nodes in the level 'it'
    void printNodes(vector<int> adj[], int n)
    {
        int count = 0;
        count = nodes_at_level[n];
        cout << count;
    }
    
    // Driver code 
    int main()
    {
        int V, x, y ,E;
        cin >> E;
        cin >> V;
    
        vector<int> adj[V+1];
    
        for (int i = 0; i < E; i++) {
            cin >> x >> y;
            addEdge(adj, x, y);
        }
        bfs(1,adj);  //assuming the start of the graph to be 1 
    
        int it;
        cin >> it;
        printNodes(adj, it);
        return 0;
    }
    

    I have seen the hackerearth question yet. But just tried to solve your problem.

    However if you want the for loop anyhow, then try this... i am just storing the vertices according to their levels ..

    vector<int> nodes_at_level[10];
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    // Taken from hackerearth.....
    
    
        int level[10];              //To determine the level of each node
        bool vis[10];               //Mark the node if visited 
    
        void bfs(int s,vector<int> adj[]) {
            queue <int> q;
            q.push(s);
            level[ s ] = 0 ;  //Setting the level of the source node as 0
            nodes_at_level[level[s]].push_back(s);
            vis[ s ] = true;
            while(!q.empty())
            {
                int p = q.front();
                q.pop();
                for(int i = 0;i < adj[ p ].size() ; i++)
                {
                    if(vis[ adj[ p ][ i ] ] == false)
                    {
                        //Setting the level of each node with an increment in the level of parent node
                        level[ adj[ p ][ i ] ] = level[ p ]+1;
    
                        nodes_at_level[level[ adj[ p ][ i ] ]].push_back(adj[p][i]);
                        //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                         q.push(adj[ p ][ i ]);
                         vis[ adj[ p ][ i ] ] = true;
                    }
                }
            }
        }
    
    //to print the number of nodes in the level 'it'
    void printNodes(vector<int> adj[], int n)
    {
        int count=0;
            for (auto x : nodes_at_level[n]){
                count++;
            } 
            cout<<count;
    }
    
    

    Hope it might help.