Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define pb push_back
- int main()
- {
- int n, m;
- cin>>n>>m;
- vector<int> a(m), b(m);
- for(auto &e: a) cin>>e;
- for(auto &e: b) cin>>e;
- vector<vector<int>> graph(n);
- for(int i=0; i<m; i++)
- {
- int u=a[i], v=b[i];
- u--, v--;
- graph[u].pb(v);
- graph[v].pb(u);
- }
- vector<int> vis(n);
- vector<int> col(n);
- int f = 1;
- function<void(int)> DFS = [&](int u)
- {
- vis[u] = 1;
- for(auto v: graph[u])
- {
- if(!vis[v])
- {
- col[v] = 1^col[u];
- DFS(v);
- }
- else
- f &= (col[u] != col[v]);
- }
- };
- for(int i=0; i<n; i++)
- {
- if(!vis[i])
- DFS(i);
- }
- if(f) cout<<"Yes\n";
- else cout<<"No\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment