Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isBipartite(vector<vector<int>>& graph) {
  4. if(graph.size()==0){
  5. return false;
  6. }
  7. unordered_set<int>first_set;
  8. unordered_set<int>second_set;
  9. queue<int>to_process_children;
  10. unordered_set<int>already_used;
  11. int first_nenul=0;
  12.  
  13. to_process_children.push(first_nenul);
  14. first_set.insert(first_nenul);
  15. while(!to_process_children.empty()){
  16. int front = to_process_children.front();
  17. to_process_children.pop();
  18. if(already_used.find(front)!=already_used.end()){
  19. continue;
  20. }
  21. if(graph[front].size()==0){
  22. continue;
  23. }
  24. if(first_set.find(front)!=first_set.end()){
  25. second_set.insert(graph[front].begin(), graph[front].end());
  26. }
  27. else{
  28. first_set.insert(graph[front].begin(), graph[front].end());
  29. }
  30. for(int i=0; i<graph[front].size();i++){
  31. to_process_children.push(graph[front][i]);
  32. }
  33. already_used.insert(front);
  34. }
  35. for(int i=0; i<graph.size();i++){
  36. if(first_set.find(i)!=first_set.end() && second_set.find(i)!= second_set.end()){
  37. return false;
  38. }
  39. }
  40. return true;
  41.  
  42. }
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement