Skip to content

Instantly share code, notes, and snippets.

@Thiago4532
Created April 28, 2020 23:25
Show Gist options
  • Select an option

  • Save Thiago4532/b51e2e9134ba5762e5767cd9f0094133 to your computer and use it in GitHub Desktop.

Select an option

Save Thiago4532/b51e2e9134ba5762e5767cd9f0094133 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
vector<int> grafo[maxn];
int cor[maxn];
void bfs(int l){
cor[l] = 0;
queue<int> fila;
fila.push(l);
while(!fila.empty()){
int u=fila.front();
fila.pop();
for(auto v : grafo[u]){
if(cor[v] == -1){
cor[v] = !cor[u];
fila.push(v);
}
}
}
}
bool bipartido(int n){
for(int i=1;i<=n;i++){
if(cor[i] == -1)
bfs(i);
}
for(int u=1;u<=n;u++){
for(auto v : grafo[u]){
if(cor[v] == cor[u]) return false;
}
}
return true;
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
int k=0;
while(cin >> n >> m && n+m){
memset(cor, -1, sizeof cor);
for(int i=0;i<maxn;i++) grafo[i].clear();
for(int i=1;i<=m;i++){
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
cout << "Instancia " << ++k << "\n";
cout << (bipartido(n) ? "sim" : "nao") << "\n\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment