MAGCARI

Bipartite

Aug 2nd, 2022
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. /*
  2.     Task    : Bipartite Graph Check
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 30 July 2022 [11:03]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. vector<int > g[100010];
  10. int color[100010];
  11. //0 - uncolor   1,2 - colored
  12. bool dfs(int now,int c){
  13.     //base case
  14.     if(color[now]){
  15.         if(color[now] == c) return true;
  16.         else                return false;
  17.     }
  18.  
  19.     //process
  20.     color[now] = c;
  21.     for(auto x:g[now]){
  22.         // 3 - c
  23.         if(!dfs(x,3-c))
  24.             return false;
  25.     }
  26.     return true;
  27. }
  28. int main(){
  29.     int n,m,u,v;
  30.     scanf("%d %d",&n,&m);
  31.     for(int i=1;i<=m;i++){
  32.         scanf("%d %d",&u,&v);
  33.         g[u].push_back(v);
  34.         g[v].push_back(u);
  35.     }
  36.     if(dfs(1,1)){
  37.         printf("Bipatite!");
  38.     }else{
  39.         printf("Not Bipartite!");
  40.     }
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment