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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

map<string, int> mp;
vector<string> nlst;

int getId(string s) {
  if (mp.count(s))
    return mp[s];
  int id = mp.size();
  nlst.push_back(s);
  return mp[s] = id;
}

int readId() {
  string s;
  cin >> s;
  return getId(s);
}

int n;
vector<int> lst[321][321];

vector<int> src[1234], dst[1234];

bool done[1234];

bool reach[321][321];

int main() {
  int A = readId(), B = readId();

  cin >> n;

  rep(it, n) {
    int m;
    cin >> m;

    rep(i, m) {
      int u = readId(), v = readId();

      lst[u][v].push_back(it);

      src[it].push_back(u);
      dst[it].push_back(v);
    }
  }

  queue<int> que;

  que.push(A), que.push(B);
  reach[A][B] = 1;

  while (!que.empty()) {
    int x = que.front();
    que.pop();
    int y = que.front();
    que.pop();
    // cout<<nlst[x]<<" "<<nlst[y]<<endl;

    rep(it, lst[x][y].size()) {
      int id = lst[x][y][it];
      if (done[id])
        continue;
      done[id] = true;

      rep(t, src[id].size()) {
        int u = src[id][t], v = dst[id][t];
        if (!reach[u][v]) {
          reach[u][v] = 1;
          que.push(u), que.push(v);
        }
      }
    }

    rep(xx, mp.size()) if (reach[xx][x] && !reach[xx][y]) {
      reach[xx][y] = 1;
      que.push(xx);
      que.push(y);
    }
    rep(yy, mp.size()) if (reach[y][yy] && !reach[x][yy]) {
      reach[x][yy] = 1;
      que.push(x);
      que.push(yy);
    }
  }

  bool ok = 1;
  rep(i, mp.size()) if (reach[i][i]) ok = 0;

  puts(ok ? "Yes" : "No");
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

map<string, int> mp;
vector<string> nlst;

int getId(string s) {
  if (mp.count(s))
    return mp[s];
  int id = mp.size();
  nlst.push_back(s);
  return mp[s] = id;
}

int readId() {
  string s;
  cin >> s;
  return getId(s);
}

int n;
vector<int> lst[321][321];
vector<int> src[1234], dst[1234];

bool done[1234];

bool reach[321][321];

int main() {
  int A = readId(), B = readId();

  cin >> n;

  rep(it, n) {
    int m;
    cin >> m;

    rep(i, m) {
      int u = readId(), v = readId();

      lst[u][v].push_back(it);

      src[it].push_back(u);
      dst[it].push_back(v);
    }
  }

  queue<int> que;

  que.push(A), que.push(B);
  reach[A][B] = 1;

  while (!que.empty()) {
    int x = que.front();
    que.pop();
    int y = que.front();
    que.pop();
    // cout<<nlst[x]<<" "<<nlst[y]<<endl;

    rep(it, lst[x][y].size()) {
      int id = lst[x][y][it];
      if (done[id])
        continue;
      done[id] = true;

      rep(t, src[id].size()) {
        int u = src[id][t], v = dst[id][t];
        if (!reach[u][v]) {
          reach[u][v] = 1;
          que.push(u), que.push(v);
        }
      }
    }

    rep(xx, n) if (reach[xx][x] && !reach[xx][y]) {
      reach[xx][y] = 1;
      que.push(xx);
      que.push(y);
    }
    rep(yy, n) if (reach[x][yy] && !reach[x][yy]) {
      reach[x][yy] = 1;
      que.push(x);
      que.push(yy);
    }
  }

  bool ok = 1;
  rep(i, mp.size()) if (reach[i][i]) ok = 0;

  puts(ok ? "Yes" : "No");
}