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

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

const ll MAX = 1000000;
const ll MOD = 1000000007;

ll fac[MAX];

// テーブルを作る前処理
/*
void COMinit() {
    fac[0] = fac[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
    }
}
*/

ll pow_kai(ll a, ll n) { // aのn乗を計算します。
  ll x = 1;
  while (n > 0) { // 全てのbitが捨てられるまで。
    if (n & 1) {  // 1番右のbitが1のとき。
      x = x * a % MOD;
    }
    a = a * a % MOD;
    n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
  }
  return x;
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  ll tmp = 1;
  for (ll i = n; i >= n - k + 1; i--)
    tmp = (tmp * i) % MOD;

  return (tmp * pow_kai(fac[k], MOD - 2)) % MOD;
}

int main() {

  ll N, M, a, b;
  cin >> N >> M;
  vector<vector<ll>> path(N + 1);

  rep(i, M) {
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  ll visited[N + 1], ans[N + 1];

  rep(i, N + 1) { visited[i] = 0; }
  visited[0] = 1;
  visited[1] = 1;

  queue<ll> list;

  ll bit = 0, now = 1;

  while (1) {

    for (ll i : path[now]) {
      if (visited[i] != 1) {
        list.push(i);
        visited[i] = 1;
        ans[i] = now;
        visited[i] = 1;
      }
    }
    if (list.empty())
      break;

    now = list.front();
    list.pop();
  }

  rep(i, N + 1) bit += visited[i];

  if (bit != N + 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; i++)
      cout << ans[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

const ll MAX = 1000000;
const ll MOD = 1000000007;

ll fac[MAX];

// テーブルを作る前処理
/*
void COMinit() {
    fac[0] = fac[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
    }
}
*/

ll pow_kai(ll a, ll n) { // aのn乗を計算します。
  ll x = 1;
  while (n > 0) { // 全てのbitが捨てられるまで。
    if (n & 1) {  // 1番右のbitが1のとき。
      x = x * a % MOD;
    }
    a = a * a % MOD;
    n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
  }
  return x;
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  ll tmp = 1;
  for (ll i = n; i >= n - k + 1; i--)
    tmp = (tmp * i) % MOD;

  return (tmp * pow_kai(fac[k], MOD - 2)) % MOD;
}

int main() {

  ll N, M;
  cin >> N >> M;
  ll path[M][2];

  rep(i, M) cin >> path[i][0] >> path[i][1];

  ll visited[N + 1], ans[N + 1];

  rep(i, N + 1) { visited[i] = 0; }
  visited[0] = 1;
  visited[1] = 1;

  queue<ll> list;

  ll bit = 0, now = 1;

  while (1) {
    rep(i, M) {
      if (path[i][0] == now) {
        if (visited[path[i][1]] == 0) {
          list.push(path[i][1]);
          visited[path[i][1]] = 1;
          ans[path[i][1]] = now;
        }
      }
      if (path[i][1] == now) {
        if (visited[path[i][0]] == 0) {
          list.push(path[i][0]);
          visited[path[i][0]] = 1;
          ans[path[i][0]] = now;
        }
      }
    }
    if (list.empty())
      break;

    now = list.front();
    list.pop();
  }

  rep(i, N + 1) bit += visited[i];

  if (bit != N + 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; i++)
      cout << ans[i] << endl;
  }

  return 0;
}