Advertisement
Falak_Ahmed_Shakib

dfs

Jan 10th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  5. ll const N=2e6;
  6. vector<ll>v[N+1];
  7. vector<ll>v1[N+1];
  8.  
  9. bool vis[N+1];
  10.  
  11. ll cost[100];
  12.  
  13. ll level[N+10];
  14.  
  15. ll level1[N+10];
  16.  
  17. bool flag,cycle;
  18.  
  19. ll sub_tree[N+10];
  20.  
  21. ll n,m,a,b;
  22.  
  23. void input()
  24. {
  25. cin>>n>>m;
  26.  
  27. ll a,b;
  28.  
  29. for(ll i=0; i<m; i++)
  30. {
  31. cin>>a>>b;
  32. v[a].push_back(b);
  33. v[b].push_back(a);
  34. }
  35. }
  36.  
  37. void DFS(ll s)
  38. {
  39. vis[s]=true;
  40. for(auto x:v[s])
  41. {
  42. if(!vis[x])
  43. {
  44. if(x==b)flag=true;
  45. vis[x]=true;
  46. DFS(x);
  47. }
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. fastread();
  54.  
  55. input();
  56. //ll a,b;
  57. cin>>a>>b;
  58. DFS(a);
  59. if(flag)cout<<"YES"<<endl;
  60. else cout<<"NO"<<endl;
  61. }
  62. /*
  63. 6 6
  64. 1 2
  65. 1 3
  66. 2 4
  67. 2 6
  68. 3 5
  69. 3 4
  70.  
  71. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement