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

#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
// max=({});
// 条件式が真ならwhileの中身を回し続ける
// printf("%d\\n", ans);

// pairの入力
// vector<pair<ll, ll>>work(n);
// rep(i, n) {
//	ll a, b;
//	cin >> a >> b;
//	work[i] = make_pair(a, b);
// for(auto p:mp)(mapの探索)
// printf("%.10f\\n",なんちゃら)
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;
  return (x / gcd(x, y) * y);
}

ll dx[8] = {0, 0, -1, 1};
ll dy[8] = {-1, 1, 0, 0};
ll dp[101010];
vector<vector<ll>> road(101010);
vector<bool> flag(101010);
vector<bool> flag2(101010);
vector<ll> ans(101010, -1);
queue<ll> q;
vector<ll> dist(101010, INF);
vector<ll> pre(101010, -1);

void bfs(ll x) {
  q.push(x);
  while (!q.empty()) {
    ll y = q.front();
    q.pop();
    for (auto nx : road[y]) {
      if (dist[nx] != INF)
        continue;
      dist[nx] = dist[y] + 1;
      pre[nx] = y;
      q.push(nx);
    }
  }
}

// long longしか使わない
int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    road[a[i]].push_back(b[i]);
    road[b[i]].push_back(a[i]);
  }
  bool flag3 = true;
  bfs(0);
  for (ll i = 1; i < n; ++i) {
    if (pre[i] == -1) {
      flag3 = false;
    }
  }
  if (flag3) {
    puts("Yes");
    for (ll i = 1; i < n; ++i) {
      cout << pre[i] + 1 << endl;
    }
  } else {
    puts("No");
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
// max=({});
// 条件式が真ならwhileの中身を回し続ける
// printf("%d\\n", ans);

// pairの入力
// vector<pair<ll, ll>>work(n);
// rep(i, n) {
//	ll a, b;
//	cin >> a >> b;
//	work[i] = make_pair(a, b);
// for(auto p:mp)(mapの探索)
// printf("%.10f\\n",なんちゃら)
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;
  return (x / gcd(x, y) * y);
}

// 素因数分解
map<ll, ll> ins;
void facterize(ll n) {
  ll a = 2;
  while (n >= a * a) {
    if (n % a == 0) {
      ins[a]++;
      ins[a] = ins[a] % 1000000007;
      n /= a;
    } else {
      a++;
    }
  }
  ins[n]++;
}

int keta(int x) {
  int y = 0;
  while (x > 0) {
    y++;
    x /= 10;
  }
  return y;
}

ll dx[8] = {0, 0, -1, 1};
ll dy[8] = {-1, 1, 0, 0};
ll dp[101010];
vector<vector<ll>> road(10101010);
vector<bool> flag(101010101);
vector<bool> flag2(101010101);
vector<ll> ans(101010101, -1);
queue<ll> q;

void bfs(ll x) {
  q.push(x);
  flag[x] = true;
  while (!q.empty()) {
    ll y = q.front();
    q.pop();
    for (auto nx : road[y]) {
      if (flag2[nx] == false) {
        ans[nx] = y;
        flag2[nx] = true;
      }
    }
    for (auto nx : road[y]) {
      if (flag[nx] == false) {
        bfs(nx);
      }
    }
  }
}

// long longしか使わない
int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    road[a[i]].push_back(b[i]);
    road[b[i]].push_back(a[i]);
  }
  bool flag3 = true;
  bfs(0);
  for (ll i = 1; i < n; ++i) {
    if (ans[i] == -1) {
      flag3 = false;
    }
  }
  if (flag3) {
    puts("Yes");
    for (ll i = 1; i < n; ++i) {
      cout << ans[i] + 1 << endl;
    }
  } else {
    puts("No");
  }
  return 0;
}