Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #define N 105
- using namespace std;
- ifstream fin("lantminim.in");
- ofstream fout("lantminim.out");
- int p,q; //cele 2 noduri date
- int a[N][N],n,m; //listele de adiacenta
- int Q[N],prim,ultim; //coada pentru BFS
- int viz[N];
- int L[N];
- int T[N];
- void Citire()
- { int x,y;
- fin>>n>>p>>q;
- while(fin>>x)
- { fin>>y;
- a[x][0]++;
- a[x][a[x][0]]=y;
- a[y][0]++;
- a[y][a[y][0]]=x;
- }
- }
- void BFS(int x)
- { int i,v,y;
- Q[1]=x; prim=ultim=1;
- viz[x]=1; L[x]=0; T[x]=0;
- while(prim<=ultim)
- { v=Q[prim]; prim++;
- for(i=1;i<=a[v][0];i++)
- { y=a[v][i];
- if(viz[y]==0)
- { viz[y]=1;
- ultim++; Q[ultim]=y;
- L[y]=L[v]+1;
- T[y]=v;
- }
- }
- }
- }
- void Lant(int x)
- { if(T[x]!=0) Lant(T[x]);
- fout<<x<<" ";
- }
- void Afisare()
- { fout<<L[q]<<"\n";
- Lant(q);
- }
- int main()
- { Citire();
- BFS(p);
- Afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement