fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int bfs(vector <int>adj[],int start,int n)
  6. {
  7. bool visited[n+1];
  8. memset(visited,0,sizeof(visited));
  9. queue <int>q;
  10. q.push(start);
  11. int node,count=0;
  12. while(!q.empty())
  13. {
  14. node=q.front();
  15. q.pop();
  16. visited[node]=1;
  17. for(int i=0;i<adj[node].size();i++)
  18. {
  19. if(visited[adj[node][i]])return 0;
  20. else
  21. {
  22. q.push(adj[node][i]);
  23. }
  24. }
  25. count++;
  26. }
  27. if(count!=n)return 0;
  28. else return 1;
  29. }
  30.  
  31.  
  32. int main() {
  33.  
  34. int n,e,u,v;
  35. cin>>n>>e;
  36. vector <int>adj[n+1];
  37. for(int i=0;i<e;i++)
  38. {
  39. cin>>u>>v;
  40. adj[u].push_back(v);
  41. }
  42.  
  43. if(bfs(adj,1,n))
  44. {
  45. cout<<"YES"<<endl;
  46. }
  47. else
  48. cout<<"NO"<<endl;
  49.  
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 15240KB
stdin
3 2
1 2 
2 3
stdout
YES