Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. #define INF 1001
  7.  
  8. vector<int> row;
  9. vector< vector<int> > lista(INF, row);
  10. vector<int> path;
  11. vector<int> sol;
  12.  
  13. int telos;
  14.  
  15. bool v[INF];
  16.  
  17. void dfs(int start)
  18. {
  19. path.push_back(start);
  20. v[start]=true;
  21. if(start==telos)
  22. {
  23. sol=path;
  24. v[start]=false;
  25. return;
  26. }
  27.  
  28. for(int i=0; i<lista[start].size(); i++)
  29. {
  30. if(v[lista[start][i]]==false)
  31. {
  32. dfs(lista[start][i]);
  33. }
  34. if(sol.size()>0)
  35. {
  36. return;
  37. }
  38. }
  39. v[start]=false;
  40. return;
  41. }
  42.  
  43. int main()
  44. {
  45. int n, a, b, l, p;
  46. cin>>a, b, n;
  47. for(int i=0; i<n; i++)
  48. {
  49. cin>>l>>p;
  50. lista[l].push_back(p);
  51. }
  52.  
  53. telos=b;
  54.  
  55. dfs(a);
  56.  
  57. if(sol.size()==0)
  58. {
  59. cout<<"-1"<<'\n';
  60. }
  61. else
  62. {
  63. for(int i=0; i<sol.size(); i++)
  64. {
  65. cout<<sol[i];
  66. }
  67. }
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement