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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define rev(v) reverse(v.begin(), v.end())
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
// #define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define dame                                                                   \\
  {                                                                            \\
    puts("-1");                                                                \\
    return 0;                                                                  \\
  }
#define YES                                                                    \\
  {                                                                            \\
    cout << "YES" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define NO                                                                     \\
  {                                                                            \\
    cout << "NO" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define Yes                                                                    \\
  {                                                                            \\
    cout << "Yes" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define No                                                                     \\
  {                                                                            \\
    cout << "No" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define OK                                                                     \\
  {                                                                            \\
    cout << "OK" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define NG                                                                     \\
  {                                                                            \\
    cout << "NG" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define sz(v) ((int)(v).size())
#define uniq(a)                                                                \\
  sort(all(a));                                                                \\
  (a).resize(unique(all(a)) - a.begin());
#define dab(v)                                                                 \\
  for (auto i : v)                                                             \\
    cout << i << ' ';                                                          \\
  cout << endl;
#define vin(v)                                                                 \\
  for (ll i = 0; i < v.size(); ++i)                                            \\
    cin >> v[i];
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void out(T a) { cout << a << endl; }
template <class T> void die(T a) {
  cout << a << endl;
  exit(0);
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }

  // reverse(path.begin(), path.end());
  return path;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  int N, M;
  cin >> N >> M;
  graph = AdjList(N);
  for (int i = 0; i < M; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(N, 0);
  // dab(prever);
  rep1(i, prever.size()) if (prever[i] == -1) No;

  out("Yes");
  rep1(i, N) {
    // vector<int> ans = get_path(i);
    // if(ans.size() == 1) No;
    cout << prever[i] + 1 << endl;
    ;
    // dab(ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define rev(v) reverse(v.begin(), v.end())
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
// #define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define dame                                                                   \\
  {                                                                            \\
    puts("-1");                                                                \\
    return 0;                                                                  \\
  }
#define YES                                                                    \\
  {                                                                            \\
    cout << "YES" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define NO                                                                     \\
  {                                                                            \\
    cout << "NO" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define Yes                                                                    \\
  {                                                                            \\
    cout << "Yes" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define No                                                                     \\
  {                                                                            \\
    cout << "No" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define OK                                                                     \\
  {                                                                            \\
    cout << "OK" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define NG                                                                     \\
  {                                                                            \\
    cout << "NG" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define sz(v) ((int)(v).size())
#define uniq(a)                                                                \\
  sort(all(a));                                                                \\
  (a).resize(unique(all(a)) - a.begin());
#define dab(v)                                                                 \\
  for (auto i : v)                                                             \\
    cout << i << ' ';                                                          \\
  cout << endl;
#define vin(v)                                                                 \\
  for (ll i = 0; i < v.size(); ++i)                                            \\
    cin >> v[i];
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void out(T a) { cout << a << endl; }
template <class T> void die(T a) {
  cout << a << endl;
  exit(0);
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

struct edge {
  int to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;

typedef pair<int, int> P;

const int INF = 10000000;

vector<int> dist;
vector<int> prever;

void dijkstra(int n, int s) {
  dist = vector<int>(n, INF);
  prever = vector<int>(n, -1);
  dist[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        prever[e.to] = v;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
}

vector<int> get_path(int t) { // 頂点tへの最短路
  vector<int> path;
  for (; t != -1; t = prever[t]) {
    path.push_back(t);
  }

  // reverse(path.begin(), path.end());
  return path;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  int N, M;
  cin >> N >> M;
  graph = AdjList(N);
  for (int i = 0; i < M; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].push_back((edge){to, 1});
    graph[to].push_back((edge){from, 1});
  }
  dijkstra(N, 0);
  // dab(prever);
  rep1(i, prever.size()) if (prever[i] == -1) No;

  out("Yes");
  rep1(i, N) {
    vector<int> ans = get_path(i);
    if (ans.size() == 1)
      No;
    cout << ans[1] + 1 << endl;
    ;
    // dab(ans);
  }

  return 0;
}