Problem p02678 (C++)

Buggy submission (status: Time Limit Exceeded):
// by Balloons
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)

using namespace std;

typedef long long LL;

const int inf = 1e9;
const int maxn = 2e5 + 5;

int n, m;
struct edges {
  int to, nxt;
} ed[maxn << 1];
int head[maxn], ecnt = 0;
int dis[maxn], pre[maxn];
struct ee {
  int to, dis;
  bool operator<(ee b) const { return dis < b.dis; }
};
priority_queue<ee> pq;

void add(int x, int y) {
  ed[++ecnt].to = y;
  ed[ecnt].nxt = head[x];
  head[x] = ecnt;
}

void dij() {
  memset(dis, 127, sizeof dis);
  pq.push((ee){1, 0});
  dis[1] = 0;
  while (!pq.empty()) {
    ee u = pq.top();
    pq.pop();
    for (int i = head[u.to]; ~i; i = ed[i].nxt) {
      int v = ed[i].to;
      if (dis[v] > dis[u.to] + 1) {
        dis[v] = dis[u.to] + 1;
        pq.push((ee){v, dis[v]});
        pre[v] = u.to;
      }
    }
  }
  //	for(int i=1;i<=n;i++)printf("%d ",dis[i]);puts("");
}

signed main() {
  memset(head, -1, sizeof head);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dij();
  int imp = dis[0];
  for (int i = 1; i <= n; i++)
    if (dis[i] == imp)
      return puts("No"), 0;
  puts("Yes");
  for (int i = 2; i <= n; i++)
    printf("%d\n", pre[i]);
  /*
  有n个点，给m个两点之间得关系，问是否每个点都能到1，是输出“Yes”，并且输出每个点到1得最短路上得前一个点，否则输出“No”。
  6 5
  1 2
  2 3
  2 4
  4 5
  2 6
  */
  return 0;
}


Fixed submission:
// by Balloons
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)

using namespace std;

typedef long long LL;

const int inf = 1e9;
const int maxn = 2e5 + 5;

int n, m;
struct edges {
  int to, nxt;
} ed[maxn << 1];
int head[maxn], ecnt = 0;
int dis[maxn], pre[maxn];
struct ee {
  int to, dis;
  bool operator<(ee b) const { return dis > b.dis; }
};
priority_queue<ee> pq;

void add(int x, int y) {
  ed[++ecnt].to = y;
  ed[ecnt].nxt = head[x];
  head[x] = ecnt;
}

void dij() {
  memset(dis, 127, sizeof dis);
  pq.push((ee){1, 0});
  dis[1] = 0;
  while (!pq.empty()) {
    ee u = pq.top();
    pq.pop();
    for (int i = head[u.to]; ~i; i = ed[i].nxt) {
      int v = ed[i].to;
      if (dis[v] > dis[u.to] + 1) {
        dis[v] = dis[u.to] + 1;
        pq.push((ee){v, dis[v]});
        pre[v] = u.to;
      }
    }
  }
  //	for(int i=1;i<=n;i++)printf("%d ",dis[i]);puts("");
}

signed main() {
  memset(head, -1, sizeof head);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  dij();
  int imp = dis[0];
  for (int i = 1; i <= n; i++)
    if (dis[i] == imp)
      return puts("No"), 0;
  puts("Yes");
  for (int i = 2; i <= n; i++)
    printf("%d\n", pre[i]);
  /*
  有n个点，给m个两点之间得关系，问是否每个点都能到1，是输出“Yes”，并且输出每个点到1得最短路上得前一个点，否则输出“No”。
  6 5
  1 2
  2 3
  2 4
  4 5
  2 6
  */
  return 0;
}