Graph Traversal Bipartite Code
Graph Traversal Bipartite Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6;
vector<bool> vis(N+1,false);
vector<int> adjlist[N+1];
while(!q.empty()) {
int current = q.front();
q.pop();
for(int child : adjlist[current]) {
if(color[child] == -1) {
color[child] = 1 - color[current];
q.push(child);
} else if(color[child] == color[current]) {
return false;
}
}
}
return true;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adjlist[u].push_back(v);
adjlist[v].push_back(u);
}
return 0;
}