fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int node,edge,zaoa_zay=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. zaoa_zay=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]==0 && zaoa_zay==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[e[i]]=mp[e[0]];
  38. id++;
  39. }
  40. if(mp.find (e[1])==mp.end())
  41. {
  42. mp[e[1]]=id;
  43. rev_mp[e[i]]=mp[e[1]];
  44. id++;
  45. }
  46. adj_list[mp[e[0]]].push_back(mp[e[1]]);
  47. }
  48. char st,ds;
  49. cin>>st>>ds;
  50. for(int i=0;i<=node;i++)
  51. {
  52. vis[i]=0;
  53. }
  54. DFS(mp[st],mp[ds]);
  55. cout<<zaoa_zay<<endl;
  56. }
Success #stdin #stdout 0.01s 5280KB
stdin
10 11
AB
BC
BD
AE
DE
DG
FE
HF
GH
GJ
HI
A
I
stdout
1