Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define mp make_pair
#define fi first
#define se second
#define fzhead EDGE(int _to, int _val, int _next)
#define fzbody to(_to), val(_val), next(_next)
#define mst(x, a) memset(x, a, sizeof(x))
#define For(i, x, y) for (int i = (x); i <= (y); i++)
using namespace std;
const int maxm = 2e5 + 10;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pa;
struct EDGE {
  int to, val, next;
  EDGE() {}
  fzhead : fzbody {}
} e[maxm << 1];
int cnt, head[maxn], vis[maxn];
int n, m;
int dis[maxn], sign[maxn];
void add(int bg, int to, int val) {
  e[++cnt] = EDGE(to, val, head[bg]);
  head[bg] = cnt;
}
void dij(int s) {
  For(i, 1, n) vis[i] = 0;
  /*
  queue<int>q;
  dis[s]=0;
  q.push(1);
  while(!q.empty())
  {
      int u=q.front();q.pop();
      for(int i=head[u]; i!=-1; i=e[i],next)
      {
          int v=e[i].to,w=e[i].val;
          if(dis[v]>dis[u]+w)
          {
              dis[v]=dis[u]+w;
              q.push(v);
              vis[v]++;
              if(vis[v])
          }
      }
  }
  */
  priority_queue<pa> q;
  dis[s] = 0;
  q.push(mp(0, s));

  while (!q.empty()) {
    int u = q.top().se;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; i != -1; i = e[i].next) {
      if (dis[e[i].to] > dis[u] + e[i].val) {
        dis[e[i].to] = dis[u] + e[i].val;
        sign[e[i].to] = u;
        q.push(mp(-dis[e[i].to], e[i].to));
      }
    }
  }
}
int main() {
  mst(dis, 0x3f);
  mst(head, -1);
  scanf("%d%d", &n, &m);
  For(i, 1, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b, 1);
    add(b, a, 1);
  }
  dij(1);
  int flag = 1;
  For(i, 2, n) if (dis[i] == 0x3f3f3f3f) {
    flag = 0;
    break;
  }
  if (flag) {
    cout << "Yes" << endl;
    For(i, 2, n) cout << sign[i] << endl;
  } else
    cout << "No" << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define mp make_pair
#define fi first
#define se second
#define fzhead EDGE(int _to, int _val, int _next)
#define fzbody to(_to), val(_val), next(_next)
#define mst(x, a) memset(x, a, sizeof(x))
#define For(i, x, y) for (int i = (x); i <= (y); i++)
using namespace std;
const int maxm = 2e5 + 10;
const int maxn = 1e5 + 10;
typedef pair<int, int> pa;
struct EDGE {
  int to, val, next;
  EDGE() {}
  fzhead : fzbody {}
} e[maxm];
int cnt, head[maxn], vis[maxn];
int n, m;
int dis[maxn];
void add(int bg, int to, int val) {
  e[++cnt] = EDGE(to, val, head[bg]);
  head[bg] = cnt;
}
void dij(int s) {
  For(i, 1, n) vis[i] = 0;
  priority_queue<pa> q;
  dis[s] = 0;
  q.push(mp(0, s));
  while (!q.empty()) {
    int u = q.top().se;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; i != -1; i = e[i].next) {
      if (dis[e[i].to] > dis[u] + e[i].val) {
        dis[e[i].to] = dis[u] + e[i].val;
        q.push(mp(-dis[e[i].to], e[i].to));
      }
    }
  }
}
int main() {
  mst(dis, 0x3f);
  mst(head, -1);
  scanf("%d%d", &n, &m);
  For(i, 1, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b, 1);
    add(b, a, 1);
  }
  dij(1);
  int flag = 0;
  For(i, 2, n) if (dis[i] != 0x3f3f3f3f) flag = 1;
  if (flag) {
    cout << "YES" << endl;
    For(i, 2, n) if (dis[i] != 0x3f3f3f3f) cout << dis[i] << endl;
  } else
    cout << "NO" << endl;
  return 0;
}