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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define mp make_pair
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define MOD 998244353
#define PI 3.14159265358979
typedef long long ll;
using namespace std;

int n, m, shirube[100005] = {};
vector<int> edge[100005];
bool isvisited[100005] = {}; // false:not visited, true:visited
queue<pair<int, int>>
    check; // first:num of visit edge, second:where is this from

void bfs() {
  while (!check.empty()) {
    pair<int, int> t = check.front();
    check.pop();
    rep(i, edge[t.first].size()) {
      if (!isvisited[edge[t.first][i]]) {
        check.push(mp(edge[t.first][i], t.first));
        shirube[edge[t.first][i]] = t.first;
        isvisited[edge[t.first][i]] = true;
      }
    }
  }
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  check.push(mp(1, 0));
  isvisited[1] = true;
  bfs();

  bool t = true;
  for (int i = 2; i <= n; i++) {
    if (shirube[i] == 0)
      t = false;
  }
  if (!t)
    No;
  else {
    Yes;
    for (int i = 2; i <= n; i++) {
      cout << shirube[i] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define mp make_pair
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define MOD 998244353
#define PI 3.14159265358979
typedef long long ll;
using namespace std;

int n, m, shirube[100005] = {};
vector<int> edge[100005];
bool isvisited[100005] = {}; // false:not visited, true:visited
queue<pair<int, int>>
    check; // first:num of visit edge, second:where is this from

void bfs() {
  while (!check.empty()) {
    pair<int, int> t = check.front();
    check.pop();
    if (!isvisited[t.first])
      shirube[t.first] = t.second;
    isvisited[t.first] = true;
    rep(i, edge[t.first].size()) {
      if (!isvisited[edge[t.first][i]])
        check.push(mp(edge[t.first][i], t.first));
    }
  }
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  check.push(mp(1, 0));
  bfs();

  bool t = true;
  for (int i = 2; i <= n; i++) {
    if (shirube[i] == 0)
      t = false;
  }
  if (!t)
    No;
  else {
    Yes;
    for (int i = 2; i <= n; i++) {
      cout << shirube[i] << endl;
    }
  }
  return 0;
}