Tuesday, August 6, 2019

Bipartite Checking (DFS) | Competitive Programming


BFS and DFS Variation

(b)  (Bipartite Checking or Bi-coloring):

Basics:
Problems:
///Bicoloring2.cpp (DFS)

int color[2020];
bool dfs(int u, int c) // color array initially -1 and c=0
{
    if(color[u]==c) return true;

    else if(color[u]==1-c) return false;

    color[u]=c;
    bool ret=true;

    for(int i=0;i<g[u].size();i++)
        ret&=dfs(g[u][i],1-c);

    return ret;
}

No comments:

Post a Comment