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

#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

int main() {
  int N;
  while (cin >> N, N) {
    map<string, int> conv;
    bool mat[201][201] = {{}}, beet[201][201] = {{}};
    int M = 200;
    UnionFind tree(M + M);

    for (int i = 0; i < M; i++)
      beet[i][i] = true;

    for (int i = 0; i < N; i++) {
      string S;
      cin >> S;
      string T = S.substr(S.find('-') + 1);
      S = S.substr(0, S.find('-'));
      if (!conv.count(S))
        conv[S] = conv.size() - 1;
      if (!conv.count(T))
        conv[T] = conv.size() - 1;
      int a = conv[S], b = conv[T];
      mat[a][b] = beet[a][b] = true;
      tree.unite(a + M, b);
      tree.unite(b + M, a);
    }
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j < M; j++) {
        if ([&]() {
              bool flag = false;
              for (int k = 0; k < M; k++) {
                if (mat[i][k] & mat[j][k])
                  flag = true;
                if (mat[k][i] & mat[k][j])
                  flag = true;
                if (mat[i][k] & mat[k][j])
                  return (false);
                if (mat[j][k] & mat[k][i])
                  return (false);
              }
              return (flag);
            }())
          beet[i][j] = beet[j][i] = true;
      }
    }
    for (int k = 0; k < M; k++) {
      for (int i = 0; i < M; i++) {
        for (int j = 0; j < M; j++) {
          beet[i][j] |= beet[i][k] & beet[k][j];
        }
      }
    }

    int Q;
    cin >> Q;
    cout << conv.size() << endl;
    while (Q--) {
      string S;
      cin >> S;
      string T = S.substr(S.find('-') + 1);
      S = S.substr(0, S.find('-'));
      if (!conv.count(S) || !conv.count(T)) {
        cout << "NO" << endl;
      } else {
        int a = conv[S];
        int b = conv[T];
        if (tree.find(a) != tree.find(b + M) ||
            tree.find(b) != tree.find(a + M) || !beet[a][b]) {
          cout << "NO" << endl;
        } else {
          cout << "YES" << endl;
        }
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

int main() {
  int N;
  while (cin >> N, N) {
    map<string, int> conv;
    bool mat[201][201] = {{}}, beet[201][201] = {{}};
    int M = 200;
    UnionFind tree(M + M);

    for (int i = 0; i < M; i++)
      beet[i][i] = true;

    for (int i = 0; i < N; i++) {
      string S;
      cin >> S;
      string T = S.substr(S.find('-') + 1);
      S = S.substr(0, S.find('-'));
      if (!conv.count(S))
        conv[S] = conv.size();
      if (!conv.count(T))
        conv[T] = conv.size();
      int a = conv[S], b = conv[T];
      mat[a][b] = beet[a][b] = true;
      tree.unite(a + M, b);
      tree.unite(b + M, a);
    }
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j < M; j++) {
        if ([&]() {
              bool flag = false;
              for (int k = 0; k < M; k++) {
                if (mat[i][k] & mat[j][k])
                  flag = true;
                if (mat[k][i] & mat[k][j])
                  flag = true;
                if (mat[i][k] & mat[k][j])
                  return (false);
                if (mat[j][k] & mat[k][i])
                  return (false);
              }
              return (flag);
            }())
          beet[i][j] = beet[j][i] = true;
      }
    }
    for (int k = 0; k < M; k++) {
      for (int i = 0; i < M; i++) {
        for (int j = 0; j < M; j++) {
          beet[i][j] |= beet[i][k] & beet[k][j];
        }
      }
    }

    int Q;
    cin >> Q;
    cout << conv.size() << endl;
    while (Q--) {
      string S;
      cin >> S;
      string T = S.substr(S.find('-') + 1);
      S = S.substr(0, S.find('-'));
      if (!conv.count(S) || !conv.count(T)) {
        cout << "NO" << endl;
      } else {
        int a = conv[S];
        int b = conv[T];
        if (tree.find(a) != tree.find(b + M) ||
            tree.find(b) != tree.find(a + M) || !beet[a][b]) {
          cout << "NO" << endl;
        } else {
          cout << "YES" << endl;
        }
      }
    }
  }
}