Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <fstream>
  2. #define N 105
  3.  
  4. using namespace std;
  5. ifstream fin("lantminim.in");
  6. ofstream fout("lantminim.out");
  7.  
  8. int p,q; //cele 2 noduri date
  9. int a[N][N],n,m; //listele de adiacenta
  10. int Q[N],prim,ultim; //coada pentru BFS
  11. int viz[N];
  12. int L[N];
  13. int T[N];
  14.  
  15. void Citire()
  16. { int x,y;
  17.   fin>>n>>p>>q;
  18.   while(fin>>x)
  19.     { fin>>y;
  20.       a[x][0]++;
  21.       a[x][a[x][0]]=y;
  22.       a[y][0]++;
  23.       a[y][a[y][0]]=x;
  24.     }
  25. }
  26.  
  27. void BFS(int x)
  28. { int i,v,y;
  29.   Q[1]=x; prim=ultim=1;
  30.   viz[x]=1; L[x]=0; T[x]=0;
  31.   while(prim<=ultim)
  32.      { v=Q[prim]; prim++;
  33.        for(i=1;i<=a[v][0];i++)
  34.           { y=a[v][i];
  35.             if(viz[y]==0)
  36.               { viz[y]=1;
  37.                 ultim++; Q[ultim]=y;
  38.                 L[y]=L[v]+1;
  39.                 T[y]=v;
  40.               }
  41.           }
  42.      }
  43. }
  44.  
  45. void Lant(int x)
  46. { if(T[x]!=0) Lant(T[x]);
  47.   fout<<x<<" ";
  48. }
  49.  
  50. void Afisare()
  51. { fout<<L[q]<<"\n";
  52.   Lant(q);
  53. }
  54.  
  55. int main()
  56. { Citire();
  57.   BFS(p);
  58.   Afisare();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement