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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 100001, M = 400004;
const int INF = 0x7f7f7f7f;
struct node {
  int i, value;
  friend bool operator<(node a, node b) { return a.value > b.value; }
};
struct edge {
  int to, value;
  int next;
} mem[M];
priority_queue<node> minq;
int head[N], siz = 0, dis[N], n, m, start, qian[N];
bool vis[N];
void dijkstra(int start) {
  dis[start] = 0;
  node tmp = {start, 0};
  minq.push(tmp);
  while (!minq.empty()) {
    tmp = minq.top();
    minq.pop();
    if (vis[tmp.i])
      continue;
    vis[tmp.i] = true;
    for (int j = head[tmp.i]; j != 0; j = mem[j].next) {
      int to = mem[j].to;
      if (!vis[to] && mem[j].value + dis[tmp.i] < dis[to]) {
        dis[to] = mem[j].value + dis[tmp.i];
        node tmp2 = {to, dis[to]};
        minq.push(tmp2);
        qian[to] = tmp.i;
      }
    }
  }
}
int main() {
  memset(head, 0, sizeof(head));
  memset(dis, 0x7f, sizeof(dis));
  memset(vis, false, sizeof(vis));
  memset(qian, -1, sizeof(qian));
  scanf("%d %d", &n, &m);
  start = 1;
  int u, v, w;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &u, &v);
    w = 1;
    siz++;
    mem[siz].to = v;
    mem[siz].value = w;
    mem[siz].next = head[u];
    head[u] = siz;
    siz++;
    mem[siz].to = u;
    mem[siz].value = w;
    mem[siz].next = head[v];
    head[v] = siz;
  }
  dijkstra(start);
  for (int i = 1; i <= n; i++) {
    if (dis[i] >= 2139062143) {
      printf("No");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++) {
    cout << qian[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 100001, M = 200002;
const int INF = 0x7f7f7f7f;
struct node {
  int i, value;
  friend bool operator<(node a, node b) { return a.value > b.value; }
};
struct edge {
  int to, value;
  int next;
} mem[M];
priority_queue<node> minq;
int head[N], siz = 0, dis[N], n, m, start, qian[N];
bool vis[N];
void dijkstra(int start) {
  dis[start] = 0;
  node tmp = {start, 0};
  minq.push(tmp);
  while (!minq.empty()) {
    tmp = minq.top();
    minq.pop();
    if (vis[tmp.i])
      continue;
    vis[tmp.i] = true;
    for (int j = head[tmp.i]; j != 0; j = mem[j].next) {
      int to = mem[j].to;
      if (!vis[to] && mem[j].value + dis[tmp.i] < dis[to]) {
        dis[to] = mem[j].value + dis[tmp.i];
        node tmp2 = {to, dis[to]};
        minq.push(tmp2);
        qian[to] = tmp.i;
      }
    }
  }
}
int main() {
  memset(head, 0, sizeof(head));
  memset(dis, 0x7f, sizeof(dis));
  memset(vis, false, sizeof(vis));
  memset(qian, -1, sizeof(qian));
  scanf("%d %d", &n, &m);
  start = 1;
  int u, v, w;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &u, &v);
    w = 1;
    siz++;
    mem[siz].to = v;
    mem[siz].value = w;
    mem[siz].next = head[u];
    head[u] = siz;
    siz++;
    mem[siz].to = u;
    mem[siz].value = w;
    mem[siz].next = head[v];
    head[v] = siz;
  }
  dijkstra(start);
  for (int i = 1; i <= n; i++) {
    if (dis[i] >= 2139062143) {
      printf("No");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; i++) {
    cout << qian[i] << endl;
  }
  return 0;
}