fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fi first
  4. #define se second
  5. #define ll long long
  6. #define all(x) (x).begin(), (x).end()
  7. #define MASK(i) (1LL<<(i))
  8. #define BIT(i,n) (((n)>>(i)) & 1LL)
  9.  
  10. template <class X, class Y> bool minimize(X& x, const Y& y) {
  11. if(x > y){ x = y; return 1; }
  12. return 0;
  13. }
  14.  
  15. template <class X, class Y> bool maximize(X& x, const Y& y) {
  16. if(x < y){ x = y; return 1; }
  17. return 0;
  18. }
  19.  
  20. const int N = 1e5 + 5;
  21.  
  22. int n, m, st, dist[N];
  23. vector<int> adj[N];
  24.  
  25. void bfs(const int &st)
  26. {
  27. queue<int> qu;
  28. dist[st] = 0;
  29. qu.push(st);
  30. while(!qu.empty()){
  31. int x = qu.front(); qu.pop();
  32. for(const auto &y : adj[x]) {
  33. if(minimize(dist[y], dist[x] + 1))
  34. qu.push(y);
  35. }
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  42. #define file "thieufilethinguhetcuu"
  43. if(fopen(file".inp","r")){
  44. freopen(file".inp","r",stdin);
  45. freopen(file".out","w",stdout);
  46. }
  47.  
  48. cin >> n >> m >> st;
  49. for(int i = 1; i <= m; ++i){
  50. int u, v; cin >> u >> v;
  51. adj[u].emplace_back(v);
  52. adj[v].emplace_back(u);
  53. }
  54.  
  55. memset(dist, 0x3f, sizeof (dist));
  56. bfs(st);
  57.  
  58. for(int i = 1; i <= n; ++i)
  59. cout << (dist[i] <= 1e9 ? dist[i] : -1) << "\n";
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 6148KB
stdin
Standard input is empty
stdout
Standard output is empty