Advertisement
dyamondz

Dos colors - P29033

Nov 29th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector< vector<int> > g;
  6.  
  7. //c=0 is one color, c=1 is another color
  8.  
  9. bool color(vector< vector<int> >&g, vector<int>& gc, int u, int c){
  10.     if(gc[u]==NULL){
  11.         gc[u]=c;
  12.         for(int v : g[u]){
  13.             if(!color(g,gc,v,1-c)) return false;
  14.         }
  15.         return true;
  16.     }
  17.     else return gc[u]==c;
  18. }
  19. bool complete_coloring(vector< vector<int> >&g, vector<int>& gc, int x){
  20.     if(x == gc.size()-1) return true;
  21.     else{
  22.         if(gc[x]==NULL){
  23.             return color(g,gc,x,0) && complete_coloring(g,gc,x+1);
  24.         }
  25.         else return complete_coloring(g,gc,x+1);
  26.     }
  27. }
  28.  
  29. int main(){
  30.     int n,m;
  31.     while(cin>>n){
  32.         cin>>m;
  33.         g = vector< vector<int> > (n);
  34.         int x,y;
  35.         for(int i=0; i<m; ++i){
  36.             cin>>x>>y;
  37.             g[x].push_back(y);
  38.             g[y].push_back(x);
  39.         }
  40.         vector<int> gc(n);
  41.         if(complete_coloring(g,gc,0)) cout<<"yes"<<endl;
  42.         else cout<<"no"<<endl;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement