Advertisement
Guest User

Untitled

a guest
May 27th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #define INF (1 << 29)
  5.  
  6. using namespace std;
  7.  
  8. struct grafo{
  9.     vector < vector <int> > adj;
  10.     vector <int> v;
  11.     int nodos, aristas, inicial;
  12.  
  13.     void leer(){
  14.         cin >> nodos >> aristas;
  15.  
  16.         adj.resize(nodos + 1);
  17.  
  18.         v = vector <int>((nodos + 1), INF);
  19.  
  20.         for(int i = 0; i < aristas; i++){
  21.             int d, h;
  22.  
  23.             cin >> d >> h;
  24.  
  25.             adj[d].push_back(h);
  26.             adj[h].push_back(d);
  27.         }
  28.  
  29.         cin >> inicial;
  30.     }
  31.  
  32.     bool DFS(const int &nodo, const int &color){
  33.         v[nodo] = color;
  34.  
  35.         for(int i = 0; i < int(adj[nodo].size()); i++){
  36.             int ady = adj[nodo][i];
  37.  
  38.             if(v[ady] == color) return false;
  39.             else if(v[ady] == INF && !DFS(ady, ((color == 1) ? 2 : 1))) return false;
  40.         }
  41.  
  42.         return true;
  43.     }
  44.  
  45.     void resolver(){
  46.         leer();
  47.  
  48.         if(DFS(inicial, 1))
  49.             cout << "Es bicoloreable" << endl;
  50.         else
  51.             cout << "No es bicoloreable" << endl;
  52.     }
  53. };
  54.  
  55. int main()
  56. {
  57.     grafo g;
  58.  
  59.     g.resolver();
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement