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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int N, M;
map<string, int> mp;
bool e[200][200], ee[200][200];
int t[200];
void dfs(int x, int c) {
  t[x] = c;
  rep(i, N) {
    if (e[x][i] && t[i] == 0)
      dfs(i, -c);
    else if (e[x][i])
      assert(t[i] == -t[x]);
  }
  rep(i, N) {
    if (e[i][x] && t[i] == 0)
      dfs(i, -c);
    else if (e[i][x])
      assert(t[i] == -t[x]);
  }
}
int main() {
  while (true) {
    cin >> M;
    if (M == 0)
      break;
    mp.clear();
    rep(i, 200) rep(j, 200) e[i][j] = 0, ee[i][j] = (i == j);
    rep(i, 200) t[i] = 0;
    int J = 0;
    rep(i, M) {
      string st;
      cin >> st;
      int j = 0;
      while (st[j] != '-')
        j++;
      string xx = st.substr(0, j), yy = st.substr(j + 1);
      if (!mp.count(xx))
        mp[xx] = J++;
      if (!mp.count(yy))
        mp[yy] = J++;
      int x = mp[xx], y = mp[yy];
      e[x][y] = 1;
    }
    N = mp.size();
    cout << N << endl;
    int I = 1;
    rep(i, N) if (t[i] == 0) dfs(i, I++);
    rep(i, N) rep(j, N) {
      bool a = 0, b = 1;
      rep(k, N) {
        if ((e[i][k] || e[k][i]) && (e[j][k] || e[k][j]))
          a = 1;
        if ((e[i][k] && e[k][j]) || (e[j][k] && e[k][i]))
          b = 0;
      }
      if (a && b)
        ee[i][j] = 1;
    }
    rep(i, N) rep(j, N) e[i][j] |= ee[i][j];
    rep(i, N) rep(j, N) rep(k, N) if (e[j][i] && e[i][k]) e[j][k] = 1;
    int Q;
    cin >> Q;
    rep(tt, Q) {
      string st;
      cin >> st;
      int j = 0;
      while (st[j] != '-')
        j++;
      string xx = st.substr(0, j), yy = st.substr(j + 1);
      if (!mp.count(xx) || !mp.count(yy)) {
        puts("NO");
        continue;
      }
      int x = mp[xx], y = mp[yy];
      if (t[x] == -t[y] && e[x][y])
        puts("YES");
      else
        puts("NO");
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(u) begin(u), end(u)
#define PB push_back
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

typedef long long int ll;
const int INF = (1 << 30) - 1;
const double EPS = 1e-9;
const int MOD = 1000000007;

int N, M;
map<string, int> mp;
bool e[200][200];
bool ee[200][200];
int t[200];

void dfs(int v, int c) {
  t[v] = c;
  for (int i = 0; i < mp.size(); i++)
    if (i != v && (e[v][i] || e[i][v])) {
      if (t[i])
        assert(t[i] == -c);
      else
        dfs(i, -c);
    }
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // cout.precision(16);
  // cout.setf(ios::fixed);

  while (cin >> N, N) {
    // init
    mp.clear();
    for (int i = 0; i < 200; i++) {
      for (int j = 0; j < 200; j++) {
        e[i][j] = ee[i][j] = i == j;
      }
      t[i] = 0;
    }

    string st;
    for (int i = 0; i < N; i++) {
      cin >> st;
      int j = 0;
      while (st[j] != '-')
        j++;
      string xx = st.substr(0, j);
      string yy = st.substr(j + 1, st.size() - j);
      if (!mp.count(xx))
        mp[xx] = mp.size();
      if (!mp.count(yy))
        mp[yy] = mp.size();
      e[mp[xx]][mp[yy]] = true;
    }

    int J = 1;
    for (int i = 0; i < mp.size(); i++) {
      if (t[i] == 0)
        dfs(i, J++);
    }

    for (int i = 0; i < mp.size(); i++) {
      for (int j = 0; j < mp.size(); j++)
        if (i != j) {
          bool fc = false;
          bool fd = false;
          for (int k = 0; k < mp.size(); k++)
            if (k != i && k != j) {
              fc |= (e[i][k] && e[j][k]) || (e[k][i] && e[k][j]);
              fd |= (e[i][k] && e[k][j]) || (e[j][k] && e[k][i]);
            }
          ee[i][j] = fc && !fd;
        }
    }

    for (int i = 0; i < mp.size(); i++)
      for (int j = 0; j < mp.size(); j++)
        e[i][j] |= ee[i][j];

    for (int k = 0; k < mp.size(); k++)
      for (int i = 0; i < mp.size(); i++)
        for (int j = 0; j < mp.size(); j++)
          e[i][j] |= e[i][k] && e[k][j];

    cin >> M;
    cout << mp.size() << endl;
    for (int i = 0; i < M; i++) {
      cin >> st;
      int j = 0;
      while (st[j] != '-')
        j++;
      string xx = st.substr(0, j);
      string yy = st.substr(j + 1, st.size() - j);

      bool flag;
      if (mp.count(xx) && mp.count(yy)) {
        int x = mp[xx], y = mp[yy];
        flag = e[x][y] && !ee[x][y] && t[x] == -t[y];
      } else {
        flag = false;
      }
      cout << (flag ? "YES" : "NO") << endl;
    }
  }

  return 0;
}