fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN = 1000005;
  5. vector<int> g[MAXN];
  6. int dist[MAXN];
  7.  
  8. int main() {
  9. ios::sync_with_stdio(false);
  10. cin.tie(nullptr);
  11.  
  12. int N, C;
  13. cin >> N >> C;
  14.  
  15. for (int i = 1; i <= C; i++) {
  16. int Ei, B1, B2;
  17. cin >> Ei >> B1 >> B2;
  18. g[Ei].push_back(B1);
  19. g[Ei].push_back(B2);
  20. }
  21.  
  22. queue<int> q;
  23. q.push(1);
  24. dist[1] = 1;
  25.  
  26. while (!q.empty()) {
  27. int u = q.front(); q.pop();
  28. for (int v : g[u]) {
  29. dist[v] = dist[u] + 1;
  30. q.push(v);
  31. }
  32. }
  33.  
  34. for (int i = 1; i <= N; i++)
  35. cout << dist[i] << "\n";
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 28444KB
stdin
5 2
3 5 4
1 2 3
stdout
1
2
2
3
3