Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("lantminim.in");
  6. ofstream fout("lantminim.out");
  7.  
  8. int a[101][101],c[101],t[101],d[101],viz[101];
  9. int n,p,q,st,dr;
  10.  
  11. void Citire()
  12. {
  13. int x,y,i;
  14. fin >> n >> p >> q;
  15. while(fin >> x >> y)
  16. {
  17. a[x][y] = a[y][x] = 1;
  18. }
  19. }
  20.  
  21. void BFS(int p, int q)
  22. {
  23. int i,k;
  24. st = dr = 1;
  25. viz[p] = 1;
  26. c[1] = p;
  27. t[p] = 0;
  28. d[p] = 1;
  29. while(st <= dr)
  30. {
  31. k = c[st];
  32. st++;
  33. for(i = 1; i<= n; i++)
  34. if(a[k][i] and !viz[i])
  35. {
  36. c[++dr] = i;
  37. viz[i] = 1;
  38. t[i] = k;
  39. d[i] = d[k] + 1;
  40. }
  41. }
  42. }
  43.  
  44. void Afis(int p)
  45. {
  46. if(p)
  47. {
  48. Afis(t[p]);
  49. fout << p <<" ";
  50. }
  51. }
  52. int main()
  53. {
  54. Citire();
  55. BFS(p,q);
  56. fout << d[q]<<"\n";
  57. Afis(q);
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement