Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool visit[1000005]={false};

void addEdge(vector<int>adj[],int u,int v,int bidir){
    adj[u].push_back(v);
    if(bidir){
        adj[v].push_back(u);
    }
}
void dfs(vector<int>adj[],int v,int node){
    visit[node]=true;
    for(int neighbour:adj[node]){
        if(!visit[neighbour])
         dfs(adj,v,neighbour);
    }
}
int connected(vector<int>adj[],int v,int node){
    int component=1;
    dfs(adj,v,node);
    for(int i=0;i<v;i++){
        if(!visit[i]){
            dfs(adj,v,i);
            component++;
        }
    }
    return component;
}
void bfs(vector<int>adj[],int v,int src){
    bool *visited=new bool[v+1]{0};
    int *dist=new int[v+1]{0};
    int *parent=new int[v+1];
    for(int i=0;i<v;i++)
    parent[i]=-1;
    queue<int>q;
    q.push(src);
    visited[src]=true;
    int node;
    while(!q.empty()){
        node=q.front();
        q.pop();
        for(auto neighbour:adj[node]){
            if(!visited[neighbour]){
            q.push(neighbour);
            visited[neighbour]=true;
            dist[neighbour]=dist[node]+1;
            parent[neighbour]=node;
            }
        }
            
    }
    int temp;
        for(int i=1;i<v;i++){
            cout<<dist[i]<<endl;
        }
}
int main(){
    int n,m;
    cin>>n>>m;
    int u,v;
    vector<int>adj[n];
    for(int i=1;i<=n;i++){
        cin>>u>>v;
        u--,v--;
        addEdge(adj,u,v,1);
    }
    int c=connected(adj,n,0);
    if(c>1) cout<<"No"<<endl;
    else{
        cout<<"Yes"<<endl;
        bfs(adj,n,0);
    }
    return 0;
}




Compiler output:
a.cc: In function 'void dfs(std::vector<int>*, int, int)':
a.cc:13:5: error: reference to 'visit' is ambiguous
   13 |     visit[node]=true;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:4:6: note:                 'bool visit [1000005]'
    4 | bool visit[1000005]={false};
      |      ^~~~~
a.cc:15:13: error: reference to 'visit' is ambiguous
   15 |         if(!visit[neighbour])
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:4:6: note:                 'bool visit [1000005]'
    4 | bool visit[1000005]={false};
      |      ^~~~~
a.cc: In function 'int connected(std::vector<int>*, int, int)':
a.cc:23:13: error: reference to 'visit' is ambiguous
   23 |         if(!visit[i]){
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:4:6: note:                 'bool visit [1000005]'
    4 | bool visit[1000005]={false};
      |      ^~~~~