Advertisement
Josif_tepe

Untitled

Nov 21st, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. int main()
  6. {
  7.     int a,b,x,y;
  8.     cin>>b;
  9.     cin>>a;
  10.     vector<int>par[b];
  11.     int boja[b];
  12.     for(int i=0;i<a;i++)
  13.     {
  14.         cin>>x>>y;
  15.         par[x].push_back(y);
  16.         par[y].push_back(x);
  17.     }
  18.     for(int i=0;i<b;i++)
  19.     {
  20.         boja[i]=-1;
  21.     }
  22.     bool t=false;
  23.     queue<int>Q;
  24.     Q.push(0);
  25.     boja[0]=0;
  26.     while(!Q.empty())
  27.     {
  28.         int teme=Q.front();
  29.         Q.pop();
  30.         if(t==true)
  31.         {
  32.             cout<<"NO";
  33.             return 0;
  34.         }
  35.         for(int i=0;i<par[teme].size();i++)
  36.         {
  37.             int sosed=par[teme][i];
  38.             int boja_za_sleden_sosed = -1;
  39.             if(boja[teme] == 0) {
  40.                 boja_za_sleden_sosed = 1;
  41.             }
  42.             else {
  43.                 boja_za_sleden_sosed = 0;
  44.             }
  45.             if(boja[sosed] == -1) {
  46.                 boja[sosed] =  boja_za_sleden_sosed;
  47.                 Q.push(sosed);
  48.             }
  49.             else if(boja[sosed] == boja[teme]) {
  50.                 t = true;
  51.                 break;
  52.             }
  53.         }
  54.     }
  55.     if(t==false)
  56.     {
  57.         cout<<"YES" << endl;
  58.     }
  59.     else {
  60.         cout << "NO" << endl;
  61.     }
  62.     return 0;
  63.     }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement