Subscribe Us

Responsive Advertisement

Advertisement

bipartite graph DSA using CPP

 

bipartite graph  DSA using CPP



#include<bits/stdc++.h>

using namespace std;

#define ll long long int

vector<ll> arr[1000];

ll vis[1000],color[1000];

bool dfs(ll v,ll col)

{

    vis[v]=1;

    color[v]=col;

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

    {

        ll child;

        child=arr[v][i];


        if(vis[child]==0)

        {

            if(dfs(child,col^1)==false)return false;


        }

        else if(color[v]==color[child])return false;

    }

    return true;

}

int main()

{


    ll edge,node;

    cin>>node>>edge;

    while(edge--)

    {

        ll x,y;

        cin>>x>>y;

        arr[x].push_back(y);

        arr[y].push_back(x);


    }

    bool r;

    r=dfs(1,1);

    if(r==true)cout<<"YES\n";

    else cout<<"NO\n";


    return 0;

}


Post a Comment

0 Comments