ummatunkh's blog

By ummatunkh, history, 4 months ago, In English

include<bits/stdc++.h>

using namespace std; int main() { int n; cin>>n; vectorv[n+5]; int e; cin>>e; for(int i=0; i<e; i++) { int x,y; cin>>x>>y; v[x].push_back(y); v[y].push_back(x); } int p,q,found=0; //cin>>p>>q; for(int j=0; j<v[q].size(); j++) { if(p==v[q][j]) { found=1; } } queueq1; int dist[n+1]; for(int i=0; i<=n; i++) { dist[i]=-1; } int s; cout<<"Enter source"; cin>>s;

q1.push(s);
dist[s]=0;
while(!q1.empty())
{
    int ax=q1.front();
    q1.pop();
    for(int i=0;i<v[ax].size();i++)
    {
        if(dist[v[ax][i]]==-1)
        {
            q1.push(v[ax][i]);
            dist[v[ax][i]]=dist[ax]+1;
        }
    }
}
for(int i=1;i<=n;i++)
{
    cout<<i<<"->"<<dist[i];
    cout<<endl;
}
cout<<endl;
for(int i=1; i<=n; i++)
{
    cout<<i<<"-> ";
    for(int j=0; j<v[i].size(); j++)
    {
        //cout<<v[i][j]<<" ";
    }
    cout<<endl;
}

}

  • Vote: I like it
  • -5
  • Vote: I do not like it