Morass

Compiled

Sep 3rd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb(x) push_back(x);
  4. #define in(y) insert(y);
  5. #define tt(t) while(t--)
  6. #define itr ::iterator it;
  7. #define ll long long
  8. #define vi vector<int>
  9. #define ii pair<int, int>
  10. #define vii vector<ii>
  11. #define si set<int>                      
  12. #define msi map<string, int>
  13. #define new cout<<endl
  14. #define ff(n) for(int i=0;i<n;i++)
  15. #define all(v) sort(v.begin(),v.end())
  16.  
  17. using namespace std;
  18. int V=0,m,n;
  19.  
  20. int colorArr[1024];
  21. bool isBP(int G[][1024], int src)
  22. {
  23.     for (int i = 0; i < V; ++i)
  24.         colorArr[i] = -1;
  25.  
  26.     colorArr[src] = 1;
  27.  
  28.     queue <int> q;
  29.     q.push(src);
  30.     while (!q.empty())
  31.     {
  32.         int u = q.front();
  33.         q.pop();
  34.         for (int v = 0; v < V; ++v)
  35.         {
  36.             if (G[u][v] && colorArr[v] == -1)
  37.             {
  38.                 // Assign alternate color to this adjacent v of u
  39.                 colorArr[v] = 1 - colorArr[u];
  40.                 q.push(v);
  41.             }
  42.             else if (G[u][v] && colorArr[v] == colorArr[u])
  43.                 return false;
  44.         }
  45.     }
  46.     return true;
  47. }
  48. int mat[1024][1024];
  49. int main(){
  50.     int t;
  51.     cin>>t;
  52.     while(t--){
  53.  
  54.         cin>>n>>m;
  55.         for(int i=0;i<V;i++)
  56.             for(int j=0;j<V;j++)
  57.                 mat[i][j]=1;
  58.  
  59.         int fr,to; 
  60.         while(m--) {
  61.             cin>>fr>>to;
  62.             mat[fr-1][to-1]=0;
  63.         }
  64.         if(isBP(mat,0)) cout<<"Yes\n";
  65.         else cout<<"No\n";
  66.  
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment