Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #pragma GCC optimize("O3")
- #define count masloriy1489
- #define y1 jhgfds
- #define ll long long
- #define double long double
- #define pb push_back
- #define eb emplace_back
- #define all(v) v.begin(), v.end()
- #define rep(i, n) for(int i = 0; i < n; i++)
- using namespace std;
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- int n, m;
- vector<int> a[100001];
- int used[100001] = {0};
- vector<int> res;
- int t = -1;
- bool f = false;
- bool g = false;
- void dfs(int x){
- used[x] = 1;
- if(f)
- return;
- for(int i : a[x]) {
- if(used[i] == 0)
- dfs(i);
- else if(used[i] == 1){
- f = true;
- t = i;
- res.push_back(x + 1);
- return;
- }
- if(f){
- if(!g)
- res.push_back(x + 1);
- if(x == t)
- g = true;
- return;
- }
- }
- used[x] = 2;
- }
- int main(){
- ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- freopen("cycle2.in", "r", stdin);
- freopen("cycle2.out", "w", stdout);
- cin >> n >> m;
- int x, y;
- for(int i = 0; i < m; i++){
- cin >> x >> y;
- x--; y--;
- if(x == y){
- cout << "YES\n" << (x+1);
- return 0;
- }
- a[x].push_back(y);
- }
- for(int i = 0; i < n; i++){
- if (!used[i]) {
- dfs(i);
- }
- }
- if(!res.empty()){
- cout << "YES\n";
- reverse(res.begin(), res.end());
- for(int &j : res) {
- cout << j << " ";
- }
- }else{
- cout << "NO";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement