Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <memory.h>
- using namespace std;
- bool uses[100000];
- vector<int> g[100000];
- vector<int> res;
- bool dfs(int a)
- {
- if(uses[a])
- {
- return true;
- }
- uses[a] = true;
- for(auto x:g[a])
- {
- cout << x << "____\n";
- if(dfs(x))
- res.push_back(x);
- }
- }
- int main()
- {
- int n, m, a, b;
- cin >> n >> m;
- for(int i = 0; i < m; i++)
- {
- cin >> a >> b;
- g[a].push_back(b);
- }
- /*for(int i = 1; i <= n; i++)
- {
- cout <<endl<< i<<": ";
- for(auto x: g[i])
- {
- cout << x << ' ';
- }
- }*/
- for(int i = 1; i <= n; i++)
- {
- if(dfs(i+1))
- {
- res.push_back(i+1);
- cout << "YES\n";
- for(auto x:res)
- {
- cout << x <<' ';
- }
- return 0;
- }
- memset(uses, 0, sizeof(uses));
- }
- cout << "NO";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement