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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el '\\n'
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define repd(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define large 1000000007
#define pb() push_back()
#define MAX 10e+7
#define float double
#define pi 3.141592653589793
void bfs(vector<int> adj[], int n, int visarr[]) {
  vector<bool> visited(n + 1, false);
  visited[1] = visited[0] = true;
  list<int> queue;
  vector<int>::iterator i;
  queue.push_back(1);
  visited[1] = true;
  while (!queue.empty()) {
    int s = queue.front();
    // visited[s]=true;
    queue.pop_front();
    for (i = adj[s].begin(); i != adj[s].end(); ++i) {
      if (!visited[*i]) {
        visarr[*i] = s;
        visited[*i] = true;
        queue.push_back(*i);
      }
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  int arr[n + 1];
  int visarr[n + 1] = {0};
  queue<int> q;
  vector<int> adj[n + 1];
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  bfs(adj, n, visarr);
  int test[n + 1];
  visarr[0] = visarr[1] = 1;
  rep(i, 0, n + 1) test[i] = visarr[i];
  sort(test, test + n + 1);
  if (test[0] == 0)
    cout << "No" << el;
  else {
    cout << "Yes" << el;
    rep(i, 2, n + 1) cout << visarr[i] << el;
  }
}
int main() {

  solve();
  return 0;
}


Error: WA

Fixed submission:

// CPP code for printing shortest path between
// two vertices of unweighted graph
#include <bits/stdc++.h>
using namespace std;
vector<int> V;
int z;
int cc = 0;
// utility function to form edge between two vertices
// source and dest
void add_edge(vector<int> adj[], int src, int dest) {
  adj[src].push_back(dest);
  adj[dest].push_back(src);
}

// a modified version of BFS that stores predecessor
// of each vertex in array p
// and its distance from source in array d
bool BFS(vector<int> adj[], int src, int dest, int v, int pred[], int dist[]) {
  // a queue to maintain queue of vertices whose
  // adjacency list is to be scanned as per normal
  // DFS algorithm
  list<int> queue;

  // boolean array visited[] which stores the
  // information whether ith vertex is reached
  // at least once in the Breadth first search
  bool visited[v];

  // initially all vertices are unvisited
  // so v[i] for all i is false
  // and as no path is yet constructed
  // dist[i] for all i set to infinity
  for (int i = 0; i < v; i++) {
    visited[i] = false;
    dist[i] = INT_MAX;
    pred[i] = -1;
  }

  // now source is first to be visited and
  // distance from source to itself should be 0
  visited[src] = true;
  dist[src] = 0;
  queue.push_back(src);

  // standard BFS algorithm
  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (int i = 0; i < adj[u].size(); i++) {
      if (visited[adj[u][i]] == false) {
        visited[adj[u][i]] = true;
        dist[adj[u][i]] = dist[u] + 1;
        pred[adj[u][i]] = u;
        queue.push_back(adj[u][i]);

        // We stop BFS when we find
        // destination.
        if (adj[u][i] == dest)
          return true;
      }
    }
  }

  return false;
}

// utility function to print the shortest distance
// between source vertex and destination vertex
void printShortestDistance(vector<int> adj[], int s, int dest, int v) {
  // predecessor[i] array stores predecessor of
  // i and distance array stores distance of i
  // from s
  int pred[v], dist[v];

  if (BFS(adj, s, dest, v, pred, dist) == false) {
    // cout << "Given source and destination"
    // 	<< " are not connected";
    return;
  }

  // vector path stores the shortest path
  vector<int> path;
  int crawl = dest;
  path.push_back(crawl);
  while (pred[crawl] != -1) {
    path.push_back(pred[crawl]);
    crawl = pred[crawl];
  }

  // distance from source is in distance array
  // cout << "Shortest path length is : "
  // 	<< dist[dest];

  // printing path from source to destination
  // cout << "\\nPath is::\\n";
  // for (int i = path.size() - 1; i >= 0; i--)
  // 	cout << path[i] << " ";
  if (path.size() > 2)
    z = path[1];
  else if (path.size() == 2) {
    z = path[0];
  }
}
bool BFS(vector<int> adj[], int s, int V) {
  // Mark all the vertices as not visited
  bool *visited = new bool[V];
  for (int i = 0; i < V; i++)
    visited[i] = false;

  // Create a queue for BFS
  list<int> queue;

  // Mark the current node as visited and enqueue it
  visited[s] = true;
  queue.push_back(s);

  // 'i' will be used to get all adjacent
  // vertices of a vertex
  vector<int>::iterator i;

  while (!queue.empty()) {
    // Dequeue a vertex from queue and print it
    s = queue.front();
    // cout << s << " ";
    queue.pop_front();

    // Get all adjacent vertices of the dequeued
    // vertex s. If a adjacent has not been visited,
    // then mark it visited and enqueue it
    for (i = adj[s].begin(); i != adj[s].end(); ++i) {
      if (!visited[*i]) {
        visited[*i] = true;
        queue.push_back(*i);
      }
    }
  }
  for (int i = 0; i < V; i++)
    if (visited[i] = false)
      return false;

  return true;
}
// Driver program to test above functions
int main() {
  // no. of vertices
  int v = 8;
  cin >> v;
  int e;
  cin >> e;
  // array of vectors is used to store the graph
  // in the form of an adjacency list
  vector<int> adj[100000];
  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    add_edge(adj, a, b);
  }
  bool b = BFS(adj, 1, v);
  if (b == false)
    cout << "no" << endl;
  else {
    cout << "Yes" << endl;

    cout << z;
    for (int i = 2; i <= v; i++) {
      printShortestDistance(adj, i, 1, v + 1);
      cout << z << endl;
      z = 0;
    }
  }
  return 0;
}