Subscribe Us

Responsive Advertisement

Advertisement

IN and Out Time of a Node | Graph Theory | DFS

 

IN and Out Time of a Node | Graph Theory | DFS 



#include<bits/stdc++.h>

using namespace std;

#define ll long long int

vector<ll> arr[10000];

ll vis[1000],in[1000],out[1000];

ll times=1;


void dfs(ll v)

{

    vis[v]=1;

    in[v]=times;

    times++;

    for(ll i=0;i<arr[v].size();i++)

    {

        ll child;

        child=arr[v][i];

        if(vis[child]==0)

        {

            dfs(child);

        }

    }

    out[v]=times++;


}

int main()

{


    ll node,edge;

    cin>>node>>edge;

    while(edge--)

    {

        ll x,y;

        cin>>x>>y;

        arr[x].push_back(y);

        arr[y].push_back(x);


    }

    dfs(1);

    for(ll i=1;i<=node;i++)

    {

        cout<<i<<"int -> "<<in[i]<<" -> "<<out[i]<<endl;

    }


    return 0;

}


Post a Comment

0 Comments