Advertisement
alsiva

ДолойСписывание

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