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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void P(int x) { cout << x << endl; }
void P(long x) { cout << x << endl; }
void P(double x) { cout << x << endl; }
void P(ll x) { cout << x << endl; }
void P(string x) { cout << x << endl; }
void P(char x) { cout << x << endl; }

#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 < (int)(n); i++)
#define drep(i, n) for (int i = n - 1; i >= 0; i--)

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

vector<int> dist;
vector<int> ans;
set<int> ok;
map<int, vector<int>> mp;

int main() {
  int n, m;
  cin >> n >> m;
  ans = vector<int>(n);
  rep(i, n) { ans[i] = INF; }

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mp[a].push_back(b);
    mp[b].push_back(a);
  }

  queue<int> q;
  rep(i, mp[0].size()) {
    ans[mp[0][i]] = 0;
    ok.insert(mp[0][i]);
    q.push(mp[0][i]);
  }

  while (!q.empty()) {
    int tmp = q.front();
    rep(i, mp[tmp].size()) {
      if (ok.find(mp[tmp][i]) == ok.end()) {
        // cout << tmp << " => " << mp[tmp][i] << endl;
        q.push(mp[tmp][i]);
        ok.insert(mp[tmp][i]);
        if (ans[mp[tmp][i]] == INF)
          ans[mp[tmp][i]] = tmp;
      }
    }
    q.pop();
  }

  for (int i = 1; i < n; i++) {
    if (ans[i] == INF) {
      No;
      return 0;
    }
  }

  Yes;
  for (int i = 1; i < n; i++) {
    P(ans[i] + 1);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void P(int x) { cout << x << endl; }
void P(long x) { cout << x << endl; }
void P(double x) { cout << x << endl; }
void P(ll x) { cout << x << endl; }
void P(string x) { cout << x << endl; }
void P(char x) { cout << x << endl; }

#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 < (int)(n); i++)
#define drep(i, n) for (int i = n - 1; i >= 0; i--)

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

vector<int> dist;
vector<int> ans;
map<int, vector<int>> mp;

int solve(int index, int count) {
  rep(i, mp[index].size()) {
    if (dist[mp[index][i]] > count) {
      dist[mp[index][i]] = count;
      ans[mp[index][i]] = index;
      solve(mp[index][i], count + 1);
    }
  }
  return 0;
}

int main() {
  int n, m;
  cin >> n >> m;
  dist = vector<int>(n);
  ans = vector<int>(n);
  rep(i, n) { dist[i] = INF; }
  dist[0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    mp[a].push_back(b);
    mp[b].push_back(a);
  }

  solve(0, 1);

  rep(i, n) {
    if (dist[i] == INF) {
      No;
      return 0;
    }
  }
  Yes;
  for (int i = 1; i < n; i++) {
    P(ans[i] + 1);
  }

  return 0;
}