Advertisement
alsiva

LookAtMe

May 21st, 2022
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. bool bfs(int start, map<int, set<int>> adjSets, vector<bool> used) {
  10.  
  11. deque<int> points;
  12. points.push_back(start);
  13. used[start] = true;
  14.  
  15. while(!points.empty()) {
  16. int current = points.front();
  17. points.pop_front();
  18.  
  19. set<int> adjSet = adjSets[current];
  20. for (auto neighbour: adjSet) {
  21. if (!used[neighbour]) {
  22. points.push_back(neighbour);
  23. used[neighbour] = true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. }
  29.  
  30. return true;
  31. }
  32.  
  33. int main() {
  34. ifstream file("input.txt");
  35. int n, m;
  36. file >> n >> m;
  37.  
  38. map<int, set<int>> adjSets;
  39. vector<bool> used;
  40. used.resize(n, false);
  41.  
  42. for (int i = 0; i < m; i++) {
  43. int from, to;
  44. file >> from;
  45. file >> to;
  46. adjSets[from-1].insert(to-1);
  47. }
  48.  
  49. for (int i = 0; i < n; i++) {
  50. if (!bfs(i, adjSets, used)) {
  51. cout << "NO" << endl;
  52. return 0;
  53. }
  54. }
  55.  
  56.  
  57. cout << "YES" << endl;
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement