Ankit_132

D

Nov 4th, 2023
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define pb     push_back
  7.  
  8. int main()
  9. {
  10.     int n, m;
  11.     cin>>n>>m;
  12.  
  13.     vector<int> a(m), b(m);
  14.     for(auto &e: a)   cin>>e;
  15.     for(auto &e: b)   cin>>e;
  16.  
  17.     vector<vector<int>> graph(n);
  18.  
  19.     for(int i=0; i<m; i++)
  20.     {
  21.         int u=a[i], v=b[i];
  22.  
  23.         u--, v--;
  24.  
  25.         graph[u].pb(v);
  26.         graph[v].pb(u);
  27.     }
  28.  
  29.     vector<int> vis(n);
  30.     vector<int> col(n);
  31.  
  32.     int f = 1;
  33.  
  34.     function<void(int)> DFS = [&](int u)
  35.     {
  36.         vis[u] = 1;
  37.  
  38.         for(auto v: graph[u])
  39.         {
  40.             if(!vis[v])
  41.             {
  42.                 col[v] = 1^col[u];
  43.                 DFS(v);
  44.             }
  45.             else
  46.                 f &= (col[u] != col[v]);
  47.         }
  48.     };
  49.  
  50.     for(int i=0; i<n; i++)
  51.     {
  52.         if(!vis[i])
  53.             DFS(i);
  54.     }
  55.  
  56.     if(f)       cout<<"Yes\n";
  57.     else        cout<<"No\n";
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment