Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<vector<int>> adjlist;
- int n,s,x;
- queue<int> q;
- vector<bool> used(n);
- vector<int> d(n),p(n);
- void bfs(int s,int u)
- {
- q.push(s);
- used[s]=true;
- p[s]=-1;
- while(!q.empty())
- {
- int v = q.front();
- q.pop();
- for (int x : adjlist[v]) {
- if (!used[x]) {
- used[x] = true;
- q.push(x);
- d[x] = d[v] + 1;
- p[x] = v;
- }
- }
- }
- if(!used[u])
- {
- cout << "NO PATHS";
- }
- else
- {
- vector<int> path;
- for(int v=u;v!=-1;v=p[v])
- {
- path.pb(v);
- }
- reverse(path.begin(),path.end());
- for(int v:path)
- {
- cout << v << " ";
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cin >> n;
- for(int i=0;i<n;i++)
- {
- vector<int> a;
- for(int j=0;j<2;j++)
- {
- int b;
- cin >> b;
- a.pb(b);
- }
- adjlist.pb(a);
- }
- cin >> s >> x;
- bfs(s,x);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement