fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define mod 1000000007
  6.  
  7. int n, m, s;
  8. vector<int> a[100005];
  9. int visited[100005];
  10. pair<int, int> cnt[100005];
  11.  
  12. void bfs( int u){
  13. queue<int> q;
  14. q.push(u); visited[u] = true;
  15. cnt[u].first = 0;
  16. while( !q.empty()){
  17. int x = q.front(); q.pop();
  18. for( int v : a[x]){
  19. if( !visited[v]){
  20. q.push(v); visited[v] = true;
  21. cnt[v].first = cnt[x].first + 1;
  22. }
  23. }
  24. }
  25. }
  26.  
  27. void inp(){
  28. cin >> n >> m >> s;
  29. int u, v;
  30. while( m--){
  31. cin >> u >> v;
  32. a[u].push_back(v);
  33. a[v].push_back(u);
  34. }
  35.  
  36. for( int i = 1; i <= 100000; i++){
  37. cnt[i].second = i;
  38. }
  39.  
  40. bfs(s);
  41.  
  42. sort( cnt + 1, cnt + n + 1);
  43. cout << s << " " << "0" << "\n";
  44. for( int i = 1; i <= n; i++){
  45. if( cnt[i].first != 0){
  46. cout << cnt[i].second << " " << cnt[i].first << "\n";
  47. }
  48. }
  49.  
  50. }
  51.  
  52. int main() {
  53. ios_base::sync_with_stdio(false);cin.tie(NULL);
  54. // freopen("a.inp", "r", stdin);
  55. // freopen("a.out", "w", stdout);
  56. inp();
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 6692KB
stdin
Standard input is empty
stdout
0 0