fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n,e;
  6. cin>>n>>e;
  7. vector<int>g[n+1];
  8. for(int i=1;i<=e;i++){
  9. int x,y;
  10. cin>>x>>y;
  11. g[x].push_back(y);
  12. g[y].push_back(x);
  13. }
  14. int s;
  15. cin>>s;
  16. queue<int>q;
  17. q.push(s);
  18. vector<int>v(n+1,0),path(n+1,0),dis(n+1,0);
  19. v[s]=1;
  20. while(!q.empty()){
  21. int front=q.front();
  22. q.pop();
  23. for(auto &it:g[front]){
  24. if(v[it]==0){
  25. v[it]=1;
  26. q.push(it);
  27. dis[it]=dis[front]+1;
  28. path[it]++;
  29. }
  30. else if(dis[it]==dis[front]+1)path[it]++;
  31. else if(dis[it]<dis[front]+1)path[it]=1;
  32. }
  33. }
  34. for(int i=1;i<=n;i++){
  35. cout<<i<<" has "<<path[i]<<" shortest path\n";
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5288KB
stdin
5 5
1 2
1 3
1 4
2 5
3 5
1
stdout
1 has 1 shortest path
2 has 1 shortest path
3 has 1 shortest path
4 has 1 shortest path
5 has 2 shortest path