Language: C++

#include <bits/stdc++.h>

using namespace std;

bool operator< (const pair<int, int> a, const pair<int, int> b){return a.first < b.first;};
//pair の1つ目の要素に従って昇順に処理したい
bool operator> (const pair<int, int> a, const pair<int, int> b){return a.first > b.first;};
//pair の2つ目の要素に従って降順に処理したい
// bool operator< (const pair<int, int> a, const pair<int, int> b){return a.second < b.second;};
// //pair の2つ目の要素に従って昇順に処理したい
// bool operator> (const pair<int, int> a, const pair<int, int> b){return a.second > b.second;};

void dijkstra(int initial, vector<bool> &visit, vector<int> &D, vector<int> &mother, vector<vector<int>> A)
{

    D[initial] = 0;
    visit[initial] = 1;

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> S;

    pair<int, int> p = make_pair(D[initial], initial);

    S.push(p);

    while (!S.empty())
    {
        int w = S.top().second;

        S.pop();

        if (A[w].size() > 0)
        {
            for (int v:A[w])
            {

                if (visit[v] ==0)
                {
                  
                    visit[v] = 1;
                }

                if (D[w] + 1 < D[A[w][i]])
                {

                    D[v] = D[w] + 1;

                    pair<int, int> new_pair = make_pair(D[v], v);
                    S.push(new_pair);

                    mother[v] = w;
                }
            }
        }
    }
}

int main()
{

    const int INF = 1001001001;

    int N, M;

    cin >> N >> M;

    //graph
    vector<vector<int>> A(N, vector<int>(0));

    // aint eady visit?
    vector<bool> visit(N, 0);

    // mother room of the room
    vector<int> mother(N);

    // cost array
    vector<int> D(N, INF);

    for (int i = 0; i < M; i++)
    {

        int b, c;

        cin >> b >> c;

        A[b - 1].push_back(c - 1);
        A[c - 1].push_back(b - 1);
    }
    //initialization
    dijkstra(0, visit, D, mother, A);
 

    for (int i = 0; i < N; i++)
    {
        if (visit[i] == 0)
        {
            cout <<"No"<< endl;
        }
    }
    

    cout << "Yes" << endl;
    for (int i = 1; i < N; i++)
    {

        cout << mother[i] + 1 << endl;
    }

    return 0;
}




// #include <bits/stdc++.h>
// #define rep(i,n) for (int i = 0; i < (n); ++i)
// using namespace std;
// using ll = long long;
// using P = pair<int,int>;
 
// const int INF = 1001001001;
// vector<int> to[100005];
 
// int main() {
//   int n, m;
//   cin >> n >> m;
//   rep(i,m) {
//     int a, b;
//     cin >> a >> b;
//     --a; --b;
//     to[a].push_back(b);
//     to[b].push_back(a);
//   }
 
//   queue<int> q;
//   vector<int> dist(n, INF);
//   vector<int> pre(n, -1);
//   dist[0] = 0;
//   q.push(0);
//   while (!q.empty()) {
//     int v = q.front(); q.pop();
//     for (int u : to[v]) {
//       if (dist[u] != INF) continue;
//       dist[u] = dist[v]+1;
//       pre[u] = v;
//       q.push(u);
//     }
//   }
 
//   cout << "Yes" << endl;
//   rep(i,n) {
//     if (i == 0) continue;
//     int ans = pre[i];
//     ++ans;
//     cout << ans << endl;
//   }
//   return 0;
// }




Compiler output:
a.cc: In function 'void dijkstra(int, std::vector<bool>&, std::vector<int>&, std::vector<int>&, std::vector<std::vector<int> >)':
a.cc:42:39: error: 'i' was not declared in this scope
   42 |                 if (D[w] + 1 < D[A[w][i]])
      |                                       ^