Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n,m;
- bool check=false;
- vector <int> g[100001];
- int color[100001];
- bool dfs(int u) {
- if (color[u] == 1)
- return true;
- if (color[u] == 2)
- return false;
- color[u] = 1;
- for (auto v : g[u]) {
- if (dfs(v))
- return true;
- }
- color[u] = 2;
- return false;
- }
- int main(){
- scanf("%d %d",&n,&m);
- for(int i=1;i<=m;i++){
- int u,v;
- scanf("%d %d",&u,&v);
- g[u].push_back(v);
- }
- for(int i=1;i<=n;i++){
- for(auto v:g[i]){
- if(dfs(v)){
- check=true;
- break;
- }
- }
- }
- if(check) printf("Yes");
- else printf("No");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement