Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
using namespace std;

struct Node {
public:
  vector<int> to_child; // first = node_Number, second = cost;
};

vi BFS(vector<Node> nodes,
       int start = 0) { // {(distance from Start, x-coordinate of
                        // max_dist,y-coordinate of max_dist}
  queue<int> q;
  vi dp(nodes.size(), pint_INF);
  vi parents(nodes.size());
  parents[start] = start;
  dp[start] = 0;
  q.push(start);

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < nodes[u].to_child.size(); i++) {
      if (dp[u] + 1 < dp[nodes[u].to_child[i]]) {
        dp[nodes[u].to_child[i]] = dp[u] + 1;
        parents[nodes[u].to_child[i]] = u;
        q.push(nodes[u].to_child[i]);
      }
    }
  }

  return parents;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<Node> nodes(N);

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    nodes[a].to_child.push_back(b);
    nodes[b].to_child.push_back(a);
  }
  vi ans = BFS(nodes, 0);

  cout << "Yes" << endl;
  for (int i = 1; i < ans.size(); i++) {
    cout << ans[i] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
using namespace std;

struct Node {
public:
  int self_number;
  int parent;
  vector<int> to_child; // first = node_Number, second = cost;
};

bool comp(const pair<int, int> a, const pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

vector<Node> Dijkstra(vector<Node> Graph_V, int start = 0) {
  vector<int> Cost(Graph_V.size(), pint_INF);
  priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&comp)> pq(
      &comp);
  Cost[start] = 0;
  Graph_V[start].parent = start;
  pq.push(make_pair(start, 0));
  while (!pq.empty()) {
    int index = pq.top().first;
    pq.pop();
    Node u = Graph_V[index];

    for (int i = 0; i < u.to_child.size(); i++) {
      if (Cost[u.self_number] + 1 < Cost[u.to_child[i]]) {
        Cost[u.to_child[i]] = Cost[u.self_number] + 1;
        Graph_V[u.to_child[i]].parent = u.self_number;
        pq.push(make_pair(u.to_child[i], Cost[u.to_child[i]]));
      }
    }
  }

  return Graph_V;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<Node> nodes(N);
  rep(i, N) { nodes[i].self_number = i; }
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    nodes[a].to_child.push_back(b);
    nodes[b].to_child.push_back(a);
  }

  vector<Node> dj = Dijkstra(nodes, 0);

  cout << "Yes" << endl;
  for (int i = 1; i < dj.size(); i++) {
    cout << dj[i].parent + 1 << endl;
  }
}