tanasaradu

Untitled

Dec 14th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 101
  3. using namespace std;
  4. ifstream fin("lant1.in");
  5. ofstream fout("lant1.out");
  6. int a[nmax][nmax],p,q,n,x,y,nsol,r;
  7. vector<int>h[nmax];
  8. queue<int>c;
  9. int viz[nmax],sol[nmax],refacere[nmax];
  10. void Citire()
  11. {
  12. fin>>n>>p>>q>>r;
  13. while(fin>>x>>y)
  14. {
  15. h[x].push_back(y);
  16. h[y].push_back(x);
  17. }
  18. }
  19. void BFS(int t)
  20. {
  21. int x,i;
  22. viz[t]=1;
  23. c.push(t);
  24. while(!c.empty())
  25. {
  26. x=c.front();
  27. c.pop();
  28. for(i=0;i<h[x].size();i++)
  29. if(!viz[h[x][i]])
  30. {
  31. viz[h[x][i]]=1;
  32. sol[h[x][i]]=x;
  33. c.push(h[x][i]);
  34. }
  35. }
  36. }
  37. void Drum(int x)
  38. {
  39. if(x==r)
  40. return;
  41. Drum(sol[x]);
  42. refacere[++nsol]=x;
  43. }
  44. void Drum1(int x)
  45. {
  46. if(x==p)
  47. {
  48. refacere[++nsol]=x;
  49. return;
  50. }
  51. Drum1(sol[x]);
  52. refacere[++nsol]=x;
  53. }
  54. int main()
  55. {
  56. int i;
  57. Citire();
  58. BFS(p);
  59. Drum1(r);
  60. for(i=1;i<=nmax;i++)
  61. sol[i]=viz[i]=0;
  62. BFS(r);
  63. Drum(q);
  64. fout<<nsol<<"\n";
  65. for(i=1;i<=nsol;i++)
  66. fout<<refacere[i]<<" ";
  67. fin.close();
  68. fout.close();
  69. return 0;
  70. }
Add Comment
Please, Sign In to add comment