Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- ifstream f ("lant1.in");
- ofstream g ("lant1.out");
- int n,p,l,pr,u,q,r,x,y,nr,i,c[101],viz[101],t[101],a[101][101];
- void lant (int x)
- {if (t[x])
- {lant(t[x]);
- g<<x<<" ";
- }
- }
- int main()
- {f>>n>>p>>q>>r;
- while (f>>x>>y)
- {
- a[x][y]=a[y][x]=1;
- }
- pr=1;
- u=1;
- c[pr]=r;
- viz[r]=1;
- t[r]=0;
- while (pr<=u)
- {x=c[pr];
- ++pr;
- for (i=1;i<=n;++i)
- if (a[i][x]==1&&viz[i]==0)
- {++u;
- c[u]=i;
- viz[i]=viz[x]+1;
- t[i]=x;
- }
- }
- g<<viz[q]+viz[p]-1<<'\n';
- x=p;
- while (x!=0)
- {g<<x<<" ";
- x=t[x];
- }
- lant(q);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement