Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4.  
  5. #define ll long long
  6. #define boost cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false);
  7. using namespace std;
  8. using graph = vector<vector<ll>>; //bits/stdc++.h
  9.  
  10. void bfs(int s, const graph& gr, vector<ll>& lvls){
  11. lvls.assign(gr.size(),-1);
  12. lvls[s] = 0;
  13. queue<ll> q;
  14. q.push(s);
  15. while(!q.empty()){
  16. ll u = q.front();
  17. q.pop();
  18. for (int v:gr[u]){
  19. if(lvls[v] != -1){
  20. continue;
  21. }
  22. lvls[v] = lvls[u] + 1;
  23. q.push(v);
  24. }
  25. }
  26. }
  27.  
  28. ll get_path(ll s, ll f, const graph& gr){
  29. vector<ll> lvls;
  30. bfs(f, gr, lvls);
  31. if(lvls[s] == -1) return -1;
  32. if(lvls[f] == -1) return -1;
  33. vector<ll> path;
  34. path.push_back(s);
  35. while(path.back()!=f){
  36. ll lvl = lvls[path.back()];
  37. for (int prev:gr[path.back()]){
  38. if (lvls[prev] == lvl - 1){
  39. path.push_back(prev);
  40. break;
  41. }
  42. }
  43. }
  44. return path.size();
  45. }
  46.  
  47. int main(){
  48. boost;
  49. ll m, n, k, s;
  50. cin >> n >> m >> k;
  51. k --;
  52. graph gr(n);
  53. for(int i = 0; i < m; ++i){
  54. ll u, v;
  55. cin >> u >> v;
  56. u -= 1;
  57. v -=1;
  58. gr[u].push_back(v);
  59. gr[v].push_back(u);
  60. }
  61. for(int i = 0; i < n; ++i){
  62. s = get_path(i, k, gr);
  63. if (s == -1){cout << s << ' ';} else{
  64. cout << s - 1 << ' ';}
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement