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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double EPS = 1e-9;
const double INF = 2147483647;
#define rep(i, n) REP(i, 0, n)
#define rep2(i, n) REP(i, 1, n + 1)
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rrep(i, n) RREP(i, 0, n)
#define RREP(i, x, n) for (int i = (int)(n)-1 - (int)(x); i >= 0; i--)

template <class T> void printv(vector<T> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << *it << ",";
  }
  cout << endl;
}

vector<int> A(2e5 + 1), B(2e5 + 1), result(2e5 + 1), length(2e5 + 1);
vector<bool> ok(2e5 + 1, false);
unordered_map<int, vector<int>> mp;
queue<int> q;

void bfs() {
  // 今いる部屋
  while (q.size() != 0) {
    int r = q.front();
    q.pop();
    // 今いる部屋につながっている部屋たち
    rep(i, mp[r].size()) {
      if (ok[mp[r][i]] == false) {
        ok[mp[r][i]] = true;
        length[mp[r][i]] = length[r] + 1;
        q.push(mp[r][i]);
        result[mp[r][i]] = r;
      }
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  rep2(i, M) { cin >> A[i] >> B[i]; }
  rep2(i, M) {
    mp[A[i]].push_back(B[i]);
    mp[B[i]].push_back(A[i]);
  }
  result[1] = 1;
  length[1] = 0;
  q.push(1);
  bfs();

  // printv(length);
  rep2(i, N) {
    if (result[i] == -1) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep2(i, N) {
    if (i == 1)
      continue;
    cout << result[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double EPS = 1e-9;
const double INF = 2147483647;
#define rep(i, n) REP(i, 0, n)
#define rep2(i, n) REP(i, 1, n + 1)
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rrep(i, n) RREP(i, 0, n)
#define RREP(i, x, n) for (int i = (int)(n)-1 - (int)(x); i >= 0; i--)

template <class T> void printv(vector<T> v) {
  for (auto it = v.begin(); it != v.end(); it++) {
    cout << *it << ",";
  }
  cout << endl;
}

vector<int> A(2e5 + 1), B(2e5 + 1), result(1e5 + 1, -1), length(1e5 + 1, INF);
vector<bool> hoge(1e5 + 1, false);
map<int, vector<int>> mp;

void dfs(int index, int depth) {
  // 今いるところが最短ならそこからつながっているところも最短なのでdfsしなくていい
  // if(hoge[index]) return;
  if (depth >= length[index] + 1)
    return;
  // indexからつながってるやつたちがmp[index]
  rep(i, mp[index].size()) {
    // if(result[mp[index][i]] != -1) continue;
    if (depth >= length[mp[index][i]])
      continue;
    result[mp[index][i]] = index;
    length[mp[index][i]] = depth + 1;
    dfs(mp[index][i], depth + 1);
  }
  // hoge[index] = true;
}

int main() {
  int N, M;
  cin >> N >> M;
  rep2(i, M) { cin >> A[i] >> B[i]; }
  rep2(i, M) {
    mp[A[i]].push_back(B[i]);
    mp[B[i]].push_back(A[i]);
  }
  result[1] = 1;
  length[1] = 0;
  hoge[1] = true;
  rep(i, mp[1].size()) {
    result[mp[1][i]] = 1;
    length[mp[1][i]] = 1;
    // 最短からつながっているところは最短
    // hoge[mp[1][i]] = true;
    dfs(mp[1][i], 1);
  }
  // printv(length);
  rep2(i, N) {
    if (result[i] == -1) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  rep2(i, N) {
    if (i == 1)
      continue;
    cout << result[i] << endl;
  }
  return 0;
}