fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int node,edge,z=0;
  4. vector<int>adj_list[20];
  5. map<char,int>mp;
  6. map<int,char>rev_mp;
  7. int vis[20];
  8. void DFS(int curr,int dest)
  9. {
  10. vis[curr]=1;
  11. if(curr==dest)
  12. {
  13. z=1;
  14. return ;
  15. }
  16. for(int i=0;i<adj_list[curr].size();i++)
  17. {
  18. int child=adj_list[curr][i];
  19. if(vis[child]==1&& z==0)
  20. {
  21. DFS(child,dest);
  22. }
  23. }
  24. return ;
  25. }
  26. int main()
  27. {
  28. int id=0;
  29. cin>>node>>edge;
  30. for(int i=1;i<=edge;i++)
  31. {
  32. string e;
  33. cin>>e;
  34. if(mp.find(e[0])==mp.end())
  35. {
  36. mp[e[0]]=id;
  37. rev_mp[id]=e[0];
  38. id++;
  39. }
  40. if(mp.find(e[1])==mp.end())
  41. {
  42. mp[e[1]]=id;
  43. rev_mp[id]=e[1];
  44. id++;
  45. }
  46. adj_list[mp[e[0]]].push_back(mp[e[1]]);
  47. }
  48. char start,dest;
  49. cin>>start>>dest;
  50. for(int i=0;i<=node;i++)
  51. {
  52. vis[i]=0;
  53. }
  54. DFS(mp[start],mp[dest]);
  55. cout<<z<<endl;
  56. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1