fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define dbg(x) cout<<#x<<" : "<<x<<endl
  5. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  6. #define maxn 100005
  7. vector<ll> edges[maxn];
  8. ll dau[maxn],d[maxn];
  9. ll n,m;
  10. void bfs(ll source){
  11. dau[source]=1;
  12. d[source]=0;
  13. queue<ll> q;
  14. q.push(source);
  15. while(!q.empty()){
  16. ll p = q.front();
  17. q.pop();
  18. for(auto v: edges[p]){
  19. if(dau[v]==1) continue;
  20. dau[v]=1;
  21. d[v]=d[p]+1;
  22. q.push(v);
  23. }
  24. }
  25. }
  26. int main(){
  27. // freopen("input.txt","r",stdin);
  28. // freopen("output.txt","w",stdout);
  29. cin>>n>>m;
  30. for(ll i=0;i<m;i++){
  31. ll u,v,t;
  32. cin>>u>>v>>t;
  33. u--,v--,t--;
  34. edges[u].push_back(v);
  35. edges[u].push_back(t);
  36. edges[v].push_back(u);
  37. edges[t].push_back(u);
  38. }
  39. bfs(0);
  40. for(ll i=0;i<n;i++) cout<<d[i]+1<<'\n';
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5968KB
stdin
Standard input is empty
stdout
Standard output is empty