Advertisement
Guest User

Untitled

a guest
Sep 21st, 2021
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. vector<vector<int>> adjlist;
  2. int n,s,x;
  3. queue<int> q;
  4. vector<bool> used(n);
  5. vector<int> d(n),p(n);
  6. void bfs(int s,int u)
  7. {
  8. q.push(s);
  9. used[s]=true;
  10. p[s]=-1;
  11. while(!q.empty())
  12. {
  13. int v = q.front();
  14. q.pop();
  15. for (int x : adjlist[v]) {
  16. if (!used[x]) {
  17. used[x] = true;
  18. q.push(x);
  19. d[x] = d[v] + 1;
  20. p[x] = v;
  21. }
  22. }
  23. }
  24. if(!used[u])
  25. {
  26. cout << "NO PATHS";
  27. }
  28. else
  29. {
  30. vector<int> path;
  31. for(int v=u;v!=-1;v=p[v])
  32. {
  33. path.pb(v);
  34. }
  35. reverse(path.begin(),path.end());
  36. for(int v:path)
  37. {
  38. cout << v << " ";
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. ios_base::sync_with_stdio(false);
  45. cin.tie(NULL);
  46. cin >> n;
  47. for(int i=0;i<n;i++)
  48. {
  49. vector<int> a;
  50. for(int j=0;j<2;j++)
  51. {
  52. int b;
  53. cin >> b;
  54. a.pb(b);
  55. }
  56. adjlist.pb(a);
  57. }
  58. cin >> s >> x;
  59. bfs(s,x);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement