Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "stdio.h"
- #include <vector>
- using namespace std;
- const int MAXN=100000;
- vector <vector <int> > v;
- short int was[MAXN];
- int k=-1;
- bool a=0;
- //vector<int> ans;
- void dfs(int u){
- was[u]=1;
- //cerr<< u + 1 << "\n";
- for (int j=0; j<(int)v[u].size()&&!a; j++){
- if (was[v[u][j]]==1){
- a=1;
- cout << "YES" << "\n";
- k=v[u][j];
- v.clear();
- v.resize(1);
- }
- else
- {
- //cerr<< u+1<<" -> "<< v[u][j]+1<<"\n";
- dfs(v[u][j]);
- }
- }
- if (a && k!=-1)
- {
- if(u==k) k=-1;
- v[0].push_back(u+1);
- }
- was[u]=2;
- }
- int main(){
- freopen("cicle.in", "r", stdin);
- freopen("cicle.out", "w", stdout);
- int n, m;
- cin >> n >> m;
- v.resize(n);
- for (int i=0, a, b; i<m; i++){
- cin >> a >> b;
- v[a-1].push_back(b-1);
- }
- for (int i=0; i<n; i++){
- if (was[i]==0){
- dfs(i);
- }
- if (a) break;
- }
- if (!a) { cout << "NO";return 0;}
- // if (ans.size()==0){ cout << "NO"; return 0;}
- // cout << k << " ";
- // cout << ans.size() << " "<< ans[ans.size()-1]<< " ";
- for (int i=v[0].size()-1; i>=0; i--){
- cout << v[0][i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement