fork download
  1. # include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. #define mod 1000000007
  5.  
  6. void dfs(vector <vector<ll>> &graph,vector <bool> &visited,ll i)
  7. {
  8. visited[i]=true;
  9. for(auto x: graph[i])
  10. {
  11. if(!visited[x])
  12. dfs(graph,visited,x);
  13. }
  14. }
  15. int main()
  16. {
  17. ios_base::sync_with_stdio(false);
  18. cin.tie(NULL);
  19. ll t,i,j,k,n;
  20. cin>>n;
  21. map <ll,ll> m;
  22. for(i=0;i<n;i++)
  23. {
  24. cin>>j;
  25. m[j]=i;
  26. }
  27. ll q;
  28. cin>>q;
  29. vector <vector<ll>> graph(n);
  30. while(q--)
  31. {
  32. ll x1,x2;
  33. cin>>x1>>x2;
  34. graph[m[x1]].push_back(m[x2]);
  35. //graph[m[x2]].push_back(m[x1]);
  36. }
  37. ll x1,x2;
  38. cin>>x1>>x2;
  39. x1=m[x1];
  40. x2=m[x2];
  41. vector <bool> visited(n,false);
  42. dfs(graph,visited,x1);
  43. if(visited[x2])
  44. cout<<"1"<<endl;
  45. else
  46. cout<<"0"<<endl;
  47. }
Success #stdin #stdout 0s 5284KB
stdin
4
2
5
7
9
4
2 9
7 2
stdout
1