Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <map>
- #include <set>
- #include <deque>
- #include <vector>
- using namespace std;
- bool bfs(int start, map<int, set<int>> adjSets, vector<int> used) {
- deque<int> points;
- points.push_back(start);
- used[start] = 1;
- while(!points.empty()) {
- int current = points.front();
- set<int> adjSet = adjSets[current];
- for (auto neighbour: adjSet) {
- if (used[neighbour] == 0) {
- points.push_back(neighbour);
- used[neighbour] = 1;
- } else {
- return false;
- }
- }
- points.pop_front();
- used[current] = 2;
- }
- return true;
- }
- int main() {
- ifstream file("input.txt");
- int n, m;
- file >> n >> m;
- map<int, set<int>> adjSets;
- vector<int> used;
- used.resize(n, 0);
- for (int i = 0; i < m; i++) {
- int from, to;
- file >> from;
- file >> to;
- adjSets[from-1].insert(to-1);
- }
- for (int i = 0; i < n; i++) {
- if (!bfs(i, adjSets, used)) {
- cout << "NO" << endl;
- return 0;
- }
- }
- cout << "YES" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement