Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define nmax 101
- using namespace std;
- ifstream fin("lant1.in");
- ofstream fout("lant1.out");
- int a[nmax][nmax],p,q,n,x,y,nsol,r;
- vector<int>h[nmax];
- queue<int>c;
- int viz[nmax],sol[nmax],refacere[nmax];
- void Citire()
- {
- fin>>n>>p>>q>>r;
- while(fin>>x>>y)
- {
- h[x].push_back(y);
- h[y].push_back(x);
- }
- }
- void BFS(int t)
- {
- int x,i;
- viz[t]=1;
- c.push(t);
- while(!c.empty())
- {
- x=c.front();
- c.pop();
- for(i=0;i<h[x].size();i++)
- if(!viz[h[x][i]])
- {
- viz[h[x][i]]=1;
- sol[h[x][i]]=x;
- c.push(h[x][i]);
- }
- }
- }
- void Drum(int x)
- {
- if(x==r)
- return;
- Drum(sol[x]);
- refacere[++nsol]=x;
- }
- void Drum1(int x)
- {
- if(x==p)
- {
- refacere[++nsol]=x;
- return;
- }
- Drum1(sol[x]);
- refacere[++nsol]=x;
- }
- int main()
- {
- int i;
- Citire();
- BFS(p);
- Drum1(r);
- for(i=1;i<=nmax;i++)
- sol[i]=viz[i]=0;
- BFS(r);
- Drum(q);
- fout<<nsol<<"\n";
- for(i=1;i<=nsol;i++)
- fout<<refacere[i]<<" ";
- fin.close();
- fout.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment