Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #define INF (1 << 29)
- using namespace std;
- struct grafo{
- vector < vector <int> > adj;
- vector <int> v;
- int nodos, aristas, inicial;
- void leer(){
- cin >> nodos >> aristas;
- adj.resize(nodos + 1);
- v = vector <int>((nodos + 1), INF);
- for(int i = 0; i < aristas; i++){
- int d, h;
- cin >> d >> h;
- adj[d].push_back(h);
- adj[h].push_back(d);
- }
- cin >> inicial;
- }
- bool DFS(const int &nodo, const int &color){
- v[nodo] = color;
- for(int i = 0; i < int(adj[nodo].size()); i++){
- int ady = adj[nodo][i];
- if(v[ady] == color) return false;
- else if(v[ady] == INF && !DFS(ady, ((color == 1) ? 2 : 1))) return false;
- }
- return true;
- }
- void resolver(){
- leer();
- if(DFS(inicial, 1))
- cout << "Es bicoloreable" << endl;
- else
- cout << "No es bicoloreable" << endl;
- }
- };
- int main()
- {
- grafo g;
- g.resolver();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement