Subscribe Us

Responsive Advertisement

Advertisement

Single Source Sortest Path (On Tree ) using DFS | Graph Theory using C++

 

Single Source Sortest Path (On Tree ) using DFS | Graph Theory  using C++



#include<bits/stdc++.h>

using namespace std;

#define ll long long int

vector<ll> arr[10000000];

ll vis[10000000],dis[10000000];

void dfs(ll v,ll d)

{

vis[v]=1;

dis[v]=d;

ll le;

le=arr[v].size();

for(ll i=0;i<le;i++)

{

ll child;

child=arr[v][i];

if(vis[child]==0)dfs(child,d+1);

}

}

int main()

{

    ll n,e,u,v,ans,i;

ans=0;

cin>>n>>e;

while(e--)

{

cin>>u>>v;

arr[u].push_back(v);

arr[v].push_back(u);

}

ll x;

cin>>x;


    dfs(x,0);

    for(i=1;i<=n;i++)

    {

        cout<<x<<"->"<<i<<"="<<dis[i]<<endl;

    }

return 0;

}


Post a Comment

0 Comments