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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

int N, M;
int V;
vector<vector<int>> E;
vector<vector<int>> rE;
vector<vector<int>> undE;

int tag[310];
int even[310];
bool same[310][310];
int adj[310][310];

void init() {
  E.clear();
  rE.clear();
  undE.clear();
  rep(i, 310) {
    tag[i] = -1;
    even[i] = -1;
    rep(j, 310) {
      same[i][j] = false;
      adj[i][j] = 0;
    }
  }
};

bool dfs(int n, int tg, int d) {
  if (tag[n] + 1)
    return false;
  tag[n] = tg;
  even[n] = d % 2;
  for (int i = 0; i < undE[n].size(); i++) {
    dfs(undE[n][i], tg, d + 1);
  }
  return true;
}

bool used[310];
bool solve(int n, int gl) {
  if (n == gl)
    return true;
  if (used[n])
    return false;
  used[n] = true;

  bool res = false;
  rep(i, rE[n].size()) {
    if (solve(rE[n][i], gl))
      res = true;
  }
  rep(i, V) {
    if (same[n][i]) {
      if (solve(i, gl))
        res = true;
    }
  }
  /*
  rep(i,E[n].size()) {
          rep(j,rE[E[n][i]].size()) {
                  if(solve(rE[E[n][i]][j], gl))
                          res = true;
          }
  }
  rep(i,rE[n].size()) {
          rep(j,E[rE[n][i]].size()) {
                  if(solve(E[rE[n][i]][j], gl))
                          res = true;
          }
  }
  // */
  return res;
}

int main() {
  while (cin >> N, N) {
    init();
    map<string, int> mp;
    for (int i = 0; i < N; i++) {
      string s, s0, s1;
      cin >> s;
      s0 = string(s.begin(), s.begin() + s.find('-'));
      s1 = string(s.begin() + s0.size() + 1, s.end());
      vector<string> vs;
      vs.pb(s0);
      vs.pb(s1);
      for (int j = 0; j < vs.size(); j++) {
        if (mp.find(vs[j]) == mp.end()) {
          int id = mp.size();
          mp[vs[j]] = id;
          E.pb(vector<int>());
          rE.pb(vector<int>());
          undE.pb(vector<int>());
        }
      }
      int a, b;
      a = mp[s0];
      b = mp[s1];
      E[b].pb(a);
      rE[a].pb(b);
      undE[b].pb(a);
      undE[a].pb(b);
      adj[b][a] = true;
    }
    V = mp.size();
    cout << V << endl;
    // for(auto it = mp.begin(); it != mp.end(); it++) cout << it->second << ":"
    // << it->first << endl;
    int tg = 0;
    for (int i = 0; i < V; i++) {
      tg += dfs(i, tg, 0);
    }
    /*
    for(int i = 0; i < 10; i++)
            cout << even[i] << " " ;
    cout << endl;
    // */

    rep(i, V) {
      rep(j, V) {
        rep(k, V) {
          if ((adj[i][k] && adj[j][k]) || (adj[k][i] && adj[k][j])) {
            same[i][j] = true;
          }
        }
        rep(k, V) {
          if ((adj[i][k] && adj[k][j]) || (adj[j][k] && adj[k][i])) {
            same[i][j] = false;
          }
        }
      }
    }

    cin >> M;
    for (int i = 0; i < M; i++) {
      rep(j, 310) used[j] = false;
      string s, ss[2];
      cin >> s;
      ss[0] = string(s.begin(), s.begin() + s.find('-'));
      ss[1] = string(s.begin() + s.find('-') + 1, s.end());
      bool res = true;
      if (mp.find(ss[0]) == mp.end() || mp.find(ss[1]) == mp.end() ||
          ss[0] == ss[1] || even[mp[ss[0]]] == even[mp[ss[1]]])
        res = false;
      if (res)
        res = solve(mp[ss[0]], mp[ss[1]]);

      cout << (res ? "YES" : "NO") << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

int N, M;
int V;
vector<vector<int>> E;
vector<vector<int>> rE;
vector<vector<int>> undE;

int tag[310];
int even[310];

void init() {
  E.clear();
  rE.clear();
  undE.clear();
  rep(i, 310) {
    tag[i] = -1;
    even[i] = -1;
  }
};

bool dfs(int n, int tg, int d) {
  if (tag[n] + 1)
    return false;
  tag[n] = tg;
  even[n] = d % 2;
  for (int i = 0; i < undE[n].size(); i++) {
    dfs(undE[n][i], tg, d + 1);
  }
  return true;
}

bool used[310];
bool solve(int n, int gl) {
  if (n == gl)
    return true;
  if (used[n])
    return false;
  used[n] = true;

  bool res = false;
  rep(i, rE[n].size()) {
    if (solve(rE[n][i], gl))
      res = true;
  }
  rep(i, E[n].size()) {
    rep(j, rE[E[n][i]].size()) {
      if (solve(rE[E[n][i]][j], gl))
        res = true;
    }
  }
  rep(i, rE[n].size()) {
    rep(j, E[rE[n][i]].size()) {
      if (solve(E[rE[n][i]][j], gl))
        res = true;
    }
  }
  return res;
}

int main() {
  while (cin >> N, N) {
    init();
    map<string, int> mp;
    for (int i = 0; i < N; i++) {
      string s, s0, s1;
      cin >> s;
      s0 = string(s.begin(), s.begin() + s.find('-'));
      s1 = string(s.begin() + s0.size() + 1, s.end());
      vector<string> vs;
      vs.pb(s0);
      vs.pb(s1);
      for (int j = 0; j < vs.size(); j++) {
        if (mp.find(vs[j]) == mp.end()) {
          mp[vs[j]] = mp.size();
          E.pb(vector<int>());
          rE.pb(vector<int>());
          undE.pb(vector<int>());
        }
      }
      int a, b;
      a = mp[s0];
      b = mp[s1];
      E[b].pb(a);
      rE[a].pb(b);
      undE[b].pb(a);
      undE[a].pb(b);
    }
    V = mp.size();
    cout << V << endl;
    // for(auto it = mp.begin(); it != mp.end(); it++) cout << it->second << ":"
    // << it->first << endl;
    int tg = 0;
    for (int i = 0; i < V; i++) {
      tg += dfs(i, tg, 0);
    }
    /*
    for(int i = 0; i < 10; i++)
            cout << even[i] << " " ;
    cout << endl;
    // */
    cin >> M;
    for (int i = 0; i < M; i++) {
      rep(j, 310) used[j] = false;
      string s, ss[2];
      cin >> s;
      ss[0] = string(s.begin(), s.begin() + s.find('-'));
      ss[1] = string(s.begin() + s.find('-') + 1, s.end());
      bool res = true;
      if (mp.find(ss[0]) == mp.end() || mp.find(ss[1]) == mp.end() ||
          ss[0] == ss[1] || even[mp[ss[0]]] == even[mp[ss[1]]])
        res = false;
      if (res)
        res = solve(mp[ss[0]], mp[ss[1]]);

      cout << (res ? "YES" : "NO") << endl;
    }
  }
}