fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n,m;
  7. cin>>n>>m;
  8. vector<vector<int>>g(n+10);
  9. vector<int>visited(n+10);
  10. vector<int>level(n+10);
  11. vector<int>ways(n+10);
  12. queue<int>q;
  13. for(int i=1;i<=m;i++)
  14. {
  15. int x,y;
  16. cin>>x>>y;
  17. g[x].push_back(y);
  18. g[y].push_back(x);
  19. }
  20. int source;
  21. cin>>source;
  22. q.push(source);
  23. visited[source]=1;
  24. ways[source]=1;
  25. while(!q.empty())
  26. {
  27. int removed=q.front();
  28. cout<<removed<<" "<<ways[removed]<<endl;
  29. q.pop();
  30. for(auto u:g[removed])
  31. {
  32. if(visited[u]==0)
  33. {
  34. q.push(u);
  35. visited[u]=1;
  36. level[u]=level[removed]+1;
  37. ways[u]=ways[removed];
  38. }
  39. else
  40. {
  41. if(level[removed]+1==level[u])
  42. {
  43. ways[u]+=ways[removed];
  44. }
  45. }
  46. }
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5276KB
stdin
5 
6
1 2
1 3
1 5
1 4 
3 6 
4 7 
1
stdout
1 1
2 1
3 1
5 1
4 1
6 1
7 1