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

#include <bits/stdc++.h>
#include <climits>
#include <iostream>
#include <math.h>

#define forn(x, n, s) for (int i = x; i < n; i += s)
#define forr(x, n, s) for (int i = x; i >= n; i -= s)
#define PI 3.14159265358979323846264338327950L

using namespace std;

void go() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
long long int mod = 1e9 + 7;

int main() {
  go();

  // code here
  int n, m;
  cin >> n >> m;
  unordered_map<int, vector<int>> List;
  unordered_set<int> st;
  forn(0, m, 1) {
    int a, b;
    cin >> a >> b;
    List[a].push_back(b);
    List[b].push_back(a);
  }
  vector<pair<int, int>> visited(n + 1, {0, INT_MAX});
  queue<pair<pair<int, int>, int>> q;
  q.push({{1, 0}, 0});
  visited[0] = {0, 0};
  st.insert(1);
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    int cur_node = node.first.first;
    int parent = node.first.second;
    if (visited[cur_node].second >= (visited[parent].second + node.second)) {
      visited[cur_node] = {parent, visited[parent].second + node.second};
    }
    for (int nei : List[cur_node]) {
      if (nei == parent)
        continue;
      if (st.find(nei) != st.end())
        continue;
      st.insert(nei);
      q.push({{nei, cur_node}, node.second + 1});
    }
  }
  int flag = 0;
  for (auto x : visited) {
    if (x.second == INT_MAX) {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    cout << "Yes" << endl;
    forn(2, n + 1, 1) cout << visited[i].first << endl;
  } else {
    cout << "No";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <climits>
#include <iostream>
#include <math.h>

#define forn(x, n, s) for (int i = x; i < n; i += s)
#define forr(x, n, s) for (int i = x; i >= n; i -= s)
#define PI 3.14159265358979323846264338327950L

using namespace std;

void go() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
long long int mod = 1e9 + 7;

int main() {
  go();

  // code here
  int n, m;
  cin >> n >> m;
  unordered_map<int, vector<int>> List;
  unordered_set<int> st;
  forn(0, m, 1) {
    int a, b;
    cin >> a >> b;
    List[a].push_back(b);
    List[b].push_back(a);
  }
  vector<pair<int, int>> visited(n + 1, {0, INT_MAX});
  queue<pair<pair<int, int>, int>> q;
  q.push({{1, 0}, 0});
  visited[0] = {0, 0};
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    int cur_node = node.first.first;
    int parent = node.first.second;
    st.insert(cur_node);
    if (visited[cur_node].second >= (visited[parent].second + node.second)) {
      visited[cur_node] = {parent, visited[parent].second + node.second};
    }
    for (int nei : List[cur_node]) {
      if (nei == parent)
        continue;
      if (st.find(nei) != st.end())
        continue;
      q.push({{nei, cur_node}, node.second + 1});
    }
  }
  int flag = 0;
  for (auto x : visited) {
    if (x.second == INT_MAX) {
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    cout << "Yes" << endl;
    forn(2, n + 1, 1) cout << visited[i].first << endl;
  } else {
    cout << "No";
  }
}