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

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
typedef long long int lli;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define REPR(i, a, b) for (int i = a; i >= b; i--)
#define adb(a)                                                                 \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define mem(a, b) memset(a, b, sizeof(a))
#define mod 998244353
#define PI 3.14159265358979323846
const ll N = 200001;

struct LessSecond {
  template <typename T, typename U>
  bool operator()(const std::pair<T, U> &x, const std::pair<T, U> &y) const {
    return x.second < y.second;
  }
};

ll power(ll a, ll b, ll m) {
  ll ans = 1;
  a = a % m;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  return power(a, m - 2, m);
}

vi g[100001];
int ans[100001];

int main() {

  fastIO;
  int test = 1;
  // cin >> test;
  while (test--) {
    int n, m;
    cin >> n >> m;
    REP(i, 0, m) {
      int u, v;
      cin >> u >> v;
      g[u].PB(v);
      g[v].PB(u);
    }
    mem(ans, 1000001);
    ans[0] = 0;
    ans[1] = 0;
    queue<int> q;
    q.push(1);
    bool vis[n + 1];
    int gates[n + 1];
    mem(gates, 1000001);
    gates[1] = 0;
    mem(vis, 0);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      vis[v] = 1;
      for (auto u : g[v]) {
        if (vis[u] == 1)
          continue;
        if (gates[u] > gates[v]) {
          ans[u] = v;
          gates[u] = gates[v];
        }
        q.push(u);
        vis[u] = 1;
      }
    }
    REP(i, 1, n + 1) {
      if (ans[i] >= 1000001) {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
    REP(i, 2, n + 1) { cout << ans[i] << endl; }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
typedef long long int lli;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define REPR(i, a, b) for (int i = a; i >= b; i--)
#define adb(a)                                                                 \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define mem(a, b) memset(a, b, sizeof(a))
#define mod 998244353
#define PI 3.14159265358979323846

vi g[100001];
int ans[100001];

int main() {

  fastIO;
  int test = 1;
  // cin >> test;
  while (test--) {
    int n, m;
    cin >> n >> m;
    REP(i, 0, m) {
      int u, v;
      cin >> u >> v;
      g[u].PB(v);
      g[v].PB(u);
    }
    mem(ans, 1000001);
    ans[0] = 0;
    ans[1] = 0;
    queue<int> q;
    q.push(1);
    bool vis[n + 1];
    int gates[n + 1];
    mem(gates, 1000001);
    gates[1] = 0;
    mem(vis, 0);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      vis[v] = 1;
      for (auto u : g[v]) {
        if (vis[u] == 1)
          continue;
        if (gates[u] > gates[v]) {
          ans[u] = v;
          gates[u] = gates[v];
        }
        q.push(u);
      }
    }
    REP(i, 1, n + 1) {
      if (ans[i] >= 1000001) {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
    REP(i, 2, n + 1) { cout << ans[i] << endl; }
  }
  return 0;
}